首页 > 解决方案 > Handle table inside another HTML table without interfering with JAVASCRIPT

问题描述

I have a table with id="mytabla", which inside contains several data, and in the last row a field called retentions that should show another table inside, in this I have no problems, thus:

<table id="mytabla" class="table table-sm table-striped">
            <tbody>
            <tr>
                <th id="#">#</th>
                <th id="Documento" class="tb-gra">Documento</th>
                <th id="Fecha">Fecha(emision)</th>
                <th id="retenciones">Retenciones</th>
            </tr>
            <tr>
                <td class="tb-neg">1</td>
                <td class="tb-gra">FACTURA</td>
                <td style="width:64pt">04/06/2018</td>
                <td>SIN RETENCION</td>
            </tr>
            <tr>
                <td class="tb-neg">1</td>
                <td class="tb-gra">FACTURA</td>
                <td style="width:64pt">04/06/2018</td>
                <td>SIN RETENCION</td>
            </tr>
            <tr>
                <td class="tb-neg">1</td>
                <td class="tb-gra">FACTURA</td>
                <td style="width:64pt">04/06/2018</td>
                <td>
                     <table><tbody>
                        <tr>
                            <td>IVA</td>
                            <td>3</td>
                            <td>120.00</td>
                            <td>100.0</td>
                            <td>120.00</td>
                       </tr>
                     </tbody></table>
                </td>
            </tr>
</tbody>
</table>

in javascript I want to select the table and count the rows, but also tell me those of the second table, as I do so that the second table does not count or do not take it into account, I use this code:

var table = document.getElementById("mytabla");
var fila=document.getElementById("mytabla").getElementsByTagName('tr');
var row=table.insertRow(parseInt(fila.length);
var cantidaddefilas=fila.length;

but he is telling me those of the second table, as I do so that he does not count those.

标签: javascripthtml

解决方案


我不确定我是否理解问题。您想计算除内表中的行之外的所有行吗?然后,而不是

var fila=document.getElementById("mytabla").getElementsByTagName('tr');

利用

var fila=document.querySelectorAll("#mytabla > tbody > tr");

此外,您不需要parseInt(fila.length). Length 属性是一个数字,这样fila.length就足够了。


推荐阅读