我有点像:

<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>

I have written jQuery to loop through each tr like:

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

但问题是它通过"tblTwo"的"tr"循环,这也是我不想要的.

推荐答案

In jQuery just use:

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

Using the children selector (>) you will walk over all the children (and not all descendents), example with three rows:

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

Result:

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

VanillaJS中,你可以使用document.querySelectorAll(),并使用forEach()在各行中穿行

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

Jquery相关问答推荐

Bootstrap 3模态框和video.js视频未正确调整大小

使用 shell 脚本判断 json 数组响应是否具有特定用户名和状态的 jq 命令

$ 不是函数 - jQuery 错误

对自定义属性执行客户端验证

jQuery $.browser 是否已弃用?

jQuery/JavaScript 碰撞检测

从 JQuery.ajax 成功数据中解析 JSON

延迟后运行功能

如何知道字体(@font-face)是否已经加载?

如何在 Angular 4 中使用 jQuery 插件?

如何在javascript中获取json键和值?

jquery可排序占位符高度问题

如何使用 jQuery 检测 IE 8?

Fancybox 不适用于 jQuery v1.9.0 [ f.browser 未定义 / 无法读取属性 'msie' ]

在未实现接口 FormData 的对象上调用附加

'touchstart' 事件是否有与点击事件相同的 e.PageX 位置?

在 Javascript/jQuery 中,(e) 是什么意思?

jQuery .val 更改不会更改输入值

在 JavaScript 开头使用分号的目的是什么?

jQuery append() 与 appendChild()