我有这个jquery选择器
$('.accordion label').live('click',function() { alert("hello clicky clicky"); }
但是如果我这样做
$('.accordion label:first').live('click',function() { alert("hello clicky clicky"); }
它的目标是第一个手风琴的标签,而不是每个手风琴的第一个标签。
有人能帮忙吗
编辑
HTML
<fieldset class="horizontal accordion"> <label class="categorylabel"><div class="accordion_open"></div>Editor Information</label> <ul> <li> <label for="editor" id="editorL"><div class="accordion_open"></div>Editor Name</label> <input id="editor" name="editor" class="force_clean" value="" type="text"> </li> </ul> </fieldset>
使用 first-child 选择器而不是 first .
first-child
first
第一 给出了整个集合中的第一个实例。
第一
第一个孩子 给出集合中的每个元素,它们是其父元素的第一个子元素。
第一个孩子
$('.accordion > label:first-child').live('click',function() { alert("hello clicky clicky"); }
或者只使用您提供的类名(假设每个手风琴的类名相同)。
$('.accordion > .categorylabel').live('click',function() { alert("hello clicky clicky"); }