2012-10-15 67 views
0

所以我想使用jQuery选择这样一个元素的非固定兄弟:的jQuery选择父

<div> 
    <ul> 
     <li>First item</li> 
     <li>Second item</li> 
     <li>Third item</li> 
    <ul> 
</div> 
<div>Item to be operated on when the first li is clicked.</div> 
<div>Item to be operated on when the second li is clicked.</div> 
<div>Item to be operated on when the third li is clicked.</div> 

当然,我期待开始我的jQuery的功能与

$('ul li') 

有没有办法做到这一点,没有多个DOM潜水?我基本需要的是“next()。next()。next()”,如果它不只是直接操作兄弟。假设我无法移动标记。谢谢。

回答

2

在这种情况下,你可能会做:

$('div').eq($(this).index()+1); 

the eq function

2

您可以使用.index()来选择对应的div元素:

var $divs = $('div'); 

$('ul li').click(function() { 
    var $div = $divs.eq($(this).index() + 1); 
}); 
+0

需要'指数()+ 1',列表中包含了'div'内以及 – billyonecan

+1

@billyonecan:此HTML需要一些类。谢谢,我会加入。 – Blender