gpt4 book ai didi

jQuery - 搜索相对于当前选择器的上一个选择器

转载 作者:行者123 更新时间:2023-12-01 01:20:21 24 4
gpt4 key购买 nike

我有一个包含 3 个部分的表单。每个部分都有一个专用的 div,用于显示有关具有焦点的输入的相当长的上下文帮助消息。

我的问题是,如果我给每个帮助 div 一个“form-tip”类,并且每个有提示的输入为“has-tip”类,我怎样才能让提示始终显示在之前的“form-tip”div?

类似于:

$('.has-tip').each(focus(function() {
var tip = $(this).attr('title');
// Find most recent $('.form-tip') in the DOM and populate it with tip
}));

我希望这是有道理的...感谢您的帮助。

最佳答案

如果 .has-tip.form-tip 的子项,那么您可以使用 .closest()像这样:

$('.has-tip').each(focus(function() {
var tip = $(this).attr('title');
var ft = $(this).closest('.form-tip');
// Find most recent $('.form-tip') in the DOM and populate it with tip
}));

如果是同级,可以使用.prev('.form-tip')或者如果它不是立即之前,那么它是 .prevAll('.form-tip: first') .

关于jQuery - 搜索相对于当前选择器的上一个选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3034370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com