So I tried this:
input.find(" tr").each(function(){ ... });
I also tried this:
input.find(" > tr").each(function(){ ... });
Both of these does not work. The first one will select any TR even if it's under a nested table. The second one will not work if the table have tbody or anything like that. Any help?
Input is defined as:
var input = $(".mySelectionScope");
The DOM would look like this:
<div class='mySelectionScope'>
<div> // Optional
<table>
<tbody>
<tr> // Select this one
<table>
<tr> // Don't select this one
....
您可以使用.not()在tr的下面过滤掉tr的内容
input.find("tr").not("tr tr")
您可以过滤它:
input.find("tr").filter(function(){return !$(this).closest('tr').length}).each(...);
What about calling first() on the list of trs?
use jQuery children() instead of find()
From the docs:
The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well.
The optional div also creates some trickiness...
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');
if tbody is always inserted in all browsers, the second if statement is not necessary, and you can instead use
rows = input.children('table').children('tbody').children('tr');
Pretty ugly, but you can't do this with selectors alone.
使用此解决方案,之间是否有tbody标签并不重要:
$('table').find('tr:first').parent().children()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.