2011-08-22 57 views
6

我有這樣的HTML結構jquery下一個沒有找到下一個div?

<img width="25" height="25" src=/data/apple_logo.jpg alt=Chocolate />  
    <input class="cake_checkboxes" style="vertical-align: bottom;" type="checkbox" name="option[232][]" value="23" id="option-value-23" /> 
    <label for="option-value-23"> Chocolate</label> 
    <br /> 
<div style='display:none;' class="quantity"> 
...... 

,如果我做

$('.cake_checkboxes:first').next('div') 

$('.cake_checkboxes:first').next('.quantity') 

我得到一個空的返回值,但如果我這樣做

$('.cake_checkboxes:first').next().next().next() 

我得到我需要的div ...什麼是錯誤的任何想法,或者如果有像jQuery closest遍歷的方法遍歷

回答

22

next()只返回DOM中的下一個元素,如果它匹配選擇器否則返回空對象。因此,您可以使用next()搜索的唯一東西是<label>。使用這個來代替:

$('.cake_checkboxes:first').nextAll('.quantity').first() 

jQuery的文檔:nextAll()

+0

我想'nextUntil()'不包括被匹配的元素? – dotjoe

+0

@dotjoe,你是對的。我修復了它 – Paulpro

+0

感謝那.. – Trace

3

兄弟姐妹也工作在這裏

$('.cake_checkboxes:first').siblings('.quantity') 
+1

兄弟姐妹稍微慢一點,因爲它也檢查起始點之前的任何元素。 nextAll只在之後檢查元素。 –