gpt4 book ai didi

javascript - 如果包含某些内容,请删除 HTML 表格标签

转载 作者:太空宇宙 更新时间:2023-11-04 15:54:19 25 4
gpt4 key购买 nike

我有 3 个表,其中一些表有子项,例如:<tr><td>...而其他人什么都没有,只有一个表标签,里面没有任何东西。我想删除空表。
注意:所有表都有一个tbldetails姓名。

var lengthtbldetail = document.getElementsByName('tbldetails');

for(var itd=0;itd<lengthtbldetail.length;itd++){
var tbdtlv = 0;

if(lengthtbldetail[itd].innerHTML.indexOf('<td>') != -1) {

// REMOVE IT...

}
}

这里一个例子来解决:

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove the following tables:-->
<table name="tbldetails">
</table>
<table name="tbldetails">
</table>
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

最佳答案

使用removeChild .您的if block 需要是

if(lengthtbldetail[itd].innerHTML.indexOf('<td>') == -1) 
{
lengthtbldetail[itd].parentNode.removeChild( lengthtbldetail[itd] );
}

演示

var lengthtbldetail = document.getElementsByTagName('table');
console.log(lengthtbldetail.length)
for (var itd = lengthtbldetail.length - 1; itd >= 0; itd--)
{
if (lengthtbldetail[itd].innerHTML.indexOf('<td>') == -1) {
lengthtbldetail[itd].parentNode.removeChild(lengthtbldetail[itd]);
}
}
<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

30 表演示

var lengthtbldetail = document.getElementsByTagName('table');
for (var itd = lengthtbldetail.length - 1; itd >= 0; itd--)
{
if (lengthtbldetail[itd].innerHTML.indexOf('<td>') == -1) {
lengthtbldetail[itd].parentNode.removeChild(lengthtbldetail[itd]);
}
}
<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

<table name="tbldetails">
<tr>
<td>
Table 1
</td>
</tr>
</table>
<br />

<!--I want to Remove this table:-->
<table name="tbldetails">
</table>
<br />

<table name="tbldetails">
<tr>
<td>
Table 3
</td>
</tr>
</table>

关于javascript - 如果包含某些内容,请删除 HTML 表格标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47157335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com