Javascript:将 ID 设置为子现有表

Javascript: Set ID to a child existing table

本文关键字:设置 ID Javascript      更新时间:2023-09-26

我需要为每个Javascript的子表分配/设置一个唯一的ID。主父表具有 ID,但子表和子表中的其他子表没有。

如何在"页面加载时"为第一个子表和第二个子表设置 ID?

<table id="main parent table">
<tbody>
    <tr>
        <td>
            <table>
                <tbody>
                    <tr>
                        <td>
                            <table>
                                <tr>
                                    <td></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </tbody>
            </table>
        </td>
    </tr>
</tbody>

您可能希望做这样的事情(在这里摆弄(:

$('table').each(function(eq, el) {
    el = $(el);
    if(typeof(el.attr('id')) === "undefined") {
        el.attr('id', 'table-' + eq);
    }
});

您检查站点上的每个表,如果它没有 id,则为树中的每个表分配"table-"的 id。

在顶部表上使用 Jquery 循环children在内部表上使用循环。 findaddClass他们。