javascript - 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.
解决方案
我不确定我是否理解问题。您想计算除内表中的行之外的所有行吗?然后,而不是
var fila=document.getElementById("mytabla").getElementsByTagName('tr');
利用
var fila=document.querySelectorAll("#mytabla > tbody > tr");
此外,您不需要parseInt(fila.length)
. Length 属性是一个数字,这样fila.length
就足够了。
推荐阅读
- javascript - 在 html 模板中使用循环的多个表,不带或带 javascript,与 e 相同的所有内容创建此表
- php - 我想让会议记录加起来,但似乎不起作用
- go - 如何从 Go 中的子命令打印所有命令?
- javascript - 检查本地 HTML 文件是否存在,如果存在则重定向到它
- python - 使用 NumPy 数组(或创建元组)的索引性能瓶颈
- listview - 在 Xamarin 的 ListView 中读取动态创建的条目值
- sql - 如何使用 PostgreSQL 窗口函数计算运行余额?
- r - 在 R 中调整 stat_cor 的 Spearman 相关性的标签输出
- python - 在pyspark中创建数据框后如何重命名所有列,根据模式转换数据类型/从csv文件中读取
- c++ - 在 Linux 上从源代码构建的 Qt5 上找不到 Qt 平台插件 xcb