我有一个包含 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/