使用replace删除javascript中所有包含id的DOM?

问题描述 投票:0回答:1

我需要导出 .xls,但表中有一些我不需要的元素。我可以使用替换来删除所有包含“id”元素的 DOM 吗?

标签

<input id="id-1" value="111"><br>
<p">111</p>
<input id="id-2" value="222"><br>
<p">222</p>
<input id="id-3" value="3"><br>
<p">333</p>
<input id="id-4" value="4"><br>
<p">444</p>
<input id="id-5" value="4"><br>
<p">555</p>
</td>`

//i want to export like:
<td>
<br>
<p">111</p>
<br>
<p">222</p>
<br>
<p">333</p>
<br>
<p">444</p>
<br>
<p">555</p>
</td>```



i tried to use replace to search all id^* element but i cant remove the dom. i want to remove all DOM which included "id" elements in xls.
javascript excel replace replaceall
1个回答
0
投票

将返回 ID 与指定模式匹配的所有元素的 NodeList,然后从 DOM 中删除每个元素

const elementsToRemove = document.querySelectorAll('[id^="id"]');

elementsToRemove.forEach(element => element.remove());
<td>
<input id="id-1" value="111"><br>
<p>111</p>
<input id="id-2" value="222"><br>
<p>222</p>
<input id="id-3" value="3"><br>
<p>333</p>
<input id="id-4" value="4"><br>
<p>444</p>
<input id="id-5" value="4"><br>
<p>555</p>
</td>

© www.soinside.com 2019 - 2024. All rights reserved.