2010-06-13 72 views
3

我有一個包含3個部分的表單。每個部分都有一個專用的div,用於處理關注焦點的輸入的相當長的上下文幫助信息。jQuery - 搜索前一個選擇器,相對於當前的

我的問題是,如果我給這些幫助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 
})); 

我希望是有道理的......感謝您的幫助。

回答

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

在.each中如果我理解正確。

2

如果.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')