简体   繁体   中英

Parent innerHTML and child nodes

我想问一下是否要删除HTML元素的子元素,但我并不一定要遍历它们,而是将父ìnnerHTMLìnnerHTML设置为null或空字符串会从内存中删除子ìnnerHTML ,而不仅是从视觉部分文献?

Yes, it will completely remove children.
For example, you have:

<div id="a_div">
<input type='button'><br>
<img src='image.png'>
</div>

Then

document.getElementById("a_div").innerHTML="<input type='button'><br><img src='image.png'>";

So if you set innerHTML to "" ( document.getElementById("a_div").innerHTML=""; ), then a_div will be

<div id="a_div">
</div>

如果您使用的是jQuery,则对父元素使用的empty()函数将从DOM中删除这些元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM