使用 jQuery 将内容添加到表中的特定列

发布于 2024-09-24 02:17:35 字数 1033 浏览 0 评论 0原文

我有一个看起来像这样的表结构。一张外面的桌子,几乎每个 td 中我都有一张里面的桌子。

<table class="outertable">
    <tr>
        <td>
            <table class="innertable">
                <tr><td></td></tr>
                <tr><td></td></tr>
            </table>
        </td>
        <td>
            <table class="innertable">
                <tr><td></td></tr>
                <tr><td></td></tr>
            </table>
        </td>
    </tr>
   .....
</table>

使用 jQuery,我需要循环遍历外部表中的每个 td,以在某些特定 td 中添加一些独特的内容。我一直在尝试使用以下代码。如果外表没有任何子表,它可以正常工作,但正如您所理解的,它不适用于子表。那么如何循环遍历表中的每个td来添加内容呢?

var row = 0;
car column = 0;
for (var i = 0; i < list.length; i++) {
    $(".outertable tr:eq(" + row + ") td:eq(" + column + ")").append(list[i].content);
    if (column == 1) {
        column = 0;
        row++;
    } else {
        column++;
    }
}

I have a table structure looking something like this. One outer table and in almost each td I got an inner table.

<table class="outertable">
    <tr>
        <td>
            <table class="innertable">
                <tr><td></td></tr>
                <tr><td></td></tr>
            </table>
        </td>
        <td>
            <table class="innertable">
                <tr><td></td></tr>
                <tr><td></td></tr>
            </table>
        </td>
    </tr>
   .....
</table>

With jQuery I need to loop through each td in the outer table to add some unique content in some specific tds. I have been trying to use the following code. It works fine if the outer table doesn't have any child tables but as you can understand it doesn't work with the child tables. So how can I loop through each td in the table to add content?

var row = 0;
car column = 0;
for (var i = 0; i < list.length; i++) {
    $(".outertable tr:eq(" + row + ") td:eq(" + column + ")").append(list[i].content);
    if (column == 1) {
        column = 0;
        row++;
    } else {
        column++;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是暖光i 2024-10-01 02:17:35

我相信你的问题是你的 CSS 选择器是贪婪的。 “.outertable tr”将获取“.outertable”下的所有子“tr”元素。相反,调整选择器以仅获取直接子级“.outertable > tr”。选择器的“td”部分也是如此。所以...

$(".outertable > tr:eq(" + row + ") > td:eq(" + column + ")").append(list[i].content);

I believe your problem is the fact that your CSS selectors are greedy. ".outertable tr" will get all children 'tr' elements under '.outertable'. Instead, adjust the selector to only get direct children ".outertable > tr". Same goes for the 'td' part of the selector. So...

$(".outertable > tr:eq(" + row + ") > td:eq(" + column + ")").append(list[i].content);
猫烠⑼条掵仅有一顆心 2024-10-01 02:17:35
$(".outertable tr td").each(function() {
   $(this).append(NEWCONTENT);
});
$(".outertable tr td").each(function() {
   $(this).append(NEWCONTENT);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文