Jquery接下来找不到下一个div?

时间:2011-08-22 20:50:23

标签: jquery

我有这个HTML结构

<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方法一样遍历

2 个答案:

答案 0 :(得分:22)

next()只返回DOM中的下一个元素(如果它与选择器匹配),否则返回空对象。因此,您使用next()搜索的唯一内容是<label>。请改用:

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

jQuery文档:nextAll()

答案 1 :(得分:3)

兄弟姐妹也在这里工作

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