开发者

jQuery - search for previous selector relative to current

开发者 https://www.devze.com 2023-01-03 13:44 出处:网络
I have a form with 3 sections in it.Each section has a dedicated div for fairly long contextual help messages regarding the input that has focus.

I have a form with 3 sections in it. Each section has a dedicated div for fairly long contextual help messages regarding the input that has focus.

My question is, if I give each of these help divs a class of "form-tip" and each input that has a tip the class "has-tip", how can I get the tip to always show up in the previous "form-开发者_JS百科tip" div?

Something like:

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

I hope that makes sense... Thanks for any help.


If the .has-tip is a child of .form-tip then you can use .closest() like this:

$('.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
}));

If it's a sibling, you can use .prev('.form-tip') or if it's not immediately before, then it's .prevAll('.form-tip: first').


var container = $(this).closest('.form-tip')

within the .each if I understood properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消