怎么在javascript中删除节点
导读:本文共1271字符,通常情况下阅读需要4分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 方法1:使用remove()删除节点remove()方法可用于删除父节点上的所有元素,包括所有文本和子节点。示例:删除ul节点 上的所有子节点li<!DOCTYPEhtml><html><head><metacharset="utf-8"></head><body&... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。方法1:使用remove()删除节点
remove()方法可用于删除父节点上的所有元素,包括所有文本和子节点。
示例:删除ul节点 上的所有子节点li
<!DOCTYPEhtml><html><head><metacharset="utf-8"></head><body><ulstyle="border:2pxdashed#006DAF;"><li>GetUpinMorning</li><li>Dosomeexercise</li><li>GetReadyforschool</li><li>StudyDaily</li><li>Dohomework</li></ul><inputid="btn"type="button"value="删除子节点"></body><script>functiondeleteChild(){vare=document.querySelector("ul");varfirst=e.firstElementChild;while(first){first.remove();first=e.firstElementChild;}}varbtn=document.getElementById("btn").onclick=function(){deleteChild();}</script></html>
效果图:
方法2:使用removeChild()删除节点
removeChild() 方法用于删除父节点上的一个子节点。
示例:删除ul节点 上的所有子节点li
<!DOCTYPEhtml><html><head><metacharset="utf-8"></head><body><ulstyle="border:2pxdashed#D58C00;"><li>GetUpinMorning</li><li>Dosomeexercise</li><li>GetReadyforschool</li><li>StudyDaily</li><li>Dohomework</li></ul><inputid="btn"type="button"value="删除子节点"></body><script>functiondeleteChild(){vare=document.querySelector("ul");varchild=e.lastElementChild;while(child){e.removeChild(child);child=e.lastElementChild;}}varbtn=document.getElementById("btn").onclick=function(){deleteChild();}</script></html>
效果图:
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
怎么在javascript中删除节点的详细内容,希望对您有所帮助,信息来源于网络。