Javascript 在jQuery中,如何选择当前表TR而不是任何嵌套表TR?
所以我试了一下:Javascript 在jQuery中,如何选择当前表TR而不是任何嵌套表TR?,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,所以我试了一下: input.find(" tr").each(function(){ ... }); input.find(" > tr").each(function(){ ... }); 我也试过: input.find(" tr").each(function(){ ... }); input.find(" > tr").each(function(){ ... }); 这两种方法都不起作用。第一个将选择任何TR,即使它位于嵌套表下。如果桌子上有tbody之类的东西,
input.find(" tr").each(function(){ ... });
input.find(" > tr").each(function(){ ... });
我也试过:
input.find(" tr").each(function(){ ... });
input.find(" > tr").each(function(){ ... });
这两种方法都不起作用。第一个将选择任何TR,即使它位于嵌套表下。如果桌子上有tbody之类的东西,第二个就不起作用了。有什么帮助吗
输入定义为:
var input = $(".mySelectionScope");
DOM将如下所示:
<div class='mySelectionScope'>
<div> // Optional
<table>
<tbody>
<tr> // Select this one
<table>
<tr> // Don't select this one
....
//可选的
//选择这个
//不要选择这个
....
您可以使用
您可以对其进行筛选:
input.find("tr").filter(function(){return !$(this).closest('tr').length}).each(...);
调用trs列表上的first()怎么样
使用jQuery children()而不是find()
从文档中:
.children()方法与.find()的不同之处在于,它只包含.children()
沿DOM树向下移动一个级别,而.find()可以遍历
向下选择多个级别以选择子元素(子元素,
等等)以及
可选的div也会产生一些诡计
var input = $('.mySelectionScope').children('div').length == 0 ?
$('.mySelectionScope') :
$('.mySelectionScope > div'),
rows = input.children('table').children('tbody').length == 0 ?
input.children('table').children('tr') :
input.children('table').children('tbody').children('tr');
如果在所有浏览器中始终插入tbody,则第二条if语句不是必需的,您可以使用
rows = input.children('table').children('tbody').children('tr');
非常难看,但单靠选择器是无法做到这一点的。使用此解决方案,无论中间是否有tbody标记:
$('table').find('tr:first').parent().children()
我需要第一个表中的所有TR,而不是嵌套表中的任何TR。