这是HTML。我不在乎它是有效的,我只想使用jQuery查询"标题要选择",仅此而已。
<div class="my class1213">
<h3> just title, no selection</h3>
<ul>
<li>title to select</li>
<li>title to select
<ul>
<li>nonono</li>
</ul>
</li>
<li>title to select
<ul>
<li>
<ul>
<li>no selection</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
我该怎么做?
$('.my ul li:first');
应该足够
替代方案:
$('.my ul li:eq(0)');
$('.my ul li:nth-child(1)');
根据未定义的评论进行编辑:
您可以使用他的解决方案或类似:
$('.my ul li').filter(function(){
return $(this).text === 'title to select';
});
您可以使用CSS选择器,也可以使用jQuery travering函数。
选项A:
$('.class1213 ul li:first-child')
// selects only the first li child of ul
选项B:
$('.class1213 ul li').first()
// first selects all li's, then only uses the first element found
$('div.my > ul > li:first-child');