jQuery表行中的每个循环 [英] jQuery each loop in table row

查看:99
本文介绍了jQuery表行中的每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何使用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,如:

I have written jQuery to loop through each tr like:

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

但问题是它循环通过tblTwo的tr,我也没有想。
任何人都可以建议解决这个问题吗?

But problem is that it loops through the "tr" of "tblTwo" also which I don't want. Can anyone please suggest something to solve this?

推荐答案

在jQuery中使用

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

使用直接子选择器(> )你将走过直接的后代(而不是所有后代)

using the direct children selector (>) you will walk over immediate descendents (and not all descendents)

in VanillaJS 你可以使用 document.querySelectorAll()并使用 forEach()

in VanillaJS you can use document.querySelectorAll() and walk over the rows using forEach()

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

这篇关于jQuery表行中的每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆