比较来自世界各地的卖家的域名和 IT 服务价格

jQuery 表行中的每个周期

我有这样的东西:


<table id="tblOne">
<tbody>
<tr>
<td>
<table id="tblTwo">
<tbody>
<tr>
<td>
Items
</td>
</tr>
<tr>
<td>
Prod
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
Item 1
</td>
</tr>
<tr>
<td>
Item 2
</td>
</tr>
</tbody>
</table>


我写 jQuery, 整理每个人 tr, 作为:


$/'#tblOne tr'/.each/function// {...code...}/;


但问题是他也经过 "tr" 的 "tblTwo", 我不想要的东西。
任何人都可以提供任何解决这个问题的东西吗?
已邀请:

喜特乐

赞同来自:

在 jQuery 只需使用:


$/'#tblOne > tbody > tr'/.each/function// {...code...}/;


使用选择器 children /
>

/, 你将通过所有的孩子元素 /而不是

全部

后代/, 例如,有三行:


$/'table > tbody > tr'/.each/function/index, tr/ { 
console.log/index/;
console.log/tr/;
}/;


结果:


0
<tr>
1
<tr>
2
<tr>




VanillaJS

您可以使用
document.querySelectorAll//

并使用行走
forEach//



[].forEach.call/document.querySelectorAll/'#tblOne &gt; tbody &gt; tr'/, function/index, tr/ {
/* console.log/index/; */
/* console.log/tr/; */
}/;


</tr></tr></tr>

裸奔

赞同来自:

只是建议:

我建议使用表的实现 DOM, 它非常简单且易于使用,你真的不需要 jQuery 为此任务。


var table = document.getElementById/'tblOne'/;

var rowLength = table.rows.length;

for/var i=0; i<rowlength; <="" accessed="" are="" as="" cell="" celllength="row.cells.length;" cells="" code="" code]="" div="" do="" easy="" every="" for="" goes="" here="" here,="" i+="1/{" looping="" over="" row="table.rows[i];" row.="" something="" var="" with="" y="0;" y+="1/{" y<celllength;="" your="" }="" }[="">
<div class="answer_text">
使用
[url=http://api.jquery.com/child-selector/]http://api.jquery.com/child-selector/[/url]
[code]&gt;

:


$/'#tblOne &gt; tbody &gt; tr'/


描述:选择所有

直接子公司

, 指定的 "child" 的
元素指示 "parent".
</div>
</rowlength;>

八刀丁二

赞同来自:

使用
http://api.jquery.com/child-selector/
>

:


$/'#tblOne > tbody > tr'/


描述:选择所有

直接子公司

, 指定的 "child" 的
元素指示 "parent".

要回复问题请先登录注册