选择子元素而不是后代 [英] Selecting children elements and not descendants
本文介绍了选择子元素而不是后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何选择塔比的孩子而不是他们的后代?例如,使用以下jQuery语句:
How can I select tbody's children and not their descendants? For example, using the following jQuery statement:
alert( $( '.someClass tbody tr' ).length );
使用下面的示例html会产生2的长度:
will yield a length of 2 using the sample html below:
<div>
<table class='someClass'>
<tbody>
<tr>
<td>
<span>cell 1-1</span>
<table>
<tbody>
<tr class='level-2'>
<td>
<span>cell 1-2</span>
<table>
<tbody>
<tr class='level-3'>
<td><span>cell 1-3</span></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<span>cell 2-1</span>
<table>
<tbody>
<tr class='level-2'>
<td>
<span>cell 2-2</span>
<table>
<tbody>
<tr class='level-3'>
<td><span>cell 2-3</span></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
推荐答案
您可以这样做:
alert( $( '.someClass > tbody > tr' ).length );
实际上是jQuery很好地实现的css2选择器.
That's actually a css2 selector, which jQuery nicely implements.
这篇关于选择子元素而不是后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文