// js 根据 id 删除 div
var thisNode = document.getElementById( "div1" );
if ( thisNode != null && thisNode.parentElement != null ) {
thisNode.parentElement.removeChild( thisNode );
}
// jquery 根据 id 删除 div
$( "#div1" ).remove();
本文介绍了使用JavaScript和jQuery两种方式来实现根据ID删除div元素的方法。JavaScript通过获取指定ID的节点并调用parentElement的removeChild方法实现;而jQuery则直接通过选择器选取元素并调用remove方法实现。
// js 根据 id 删除 div
var thisNode = document.getElementById( "div1" );
if ( thisNode != null && thisNode.parentElement != null ) {
thisNode.parentElement.removeChild( thisNode );
}
// jquery 根据 id 删除 div
$( "#div1" ).remove();
1975

被折叠的 条评论
为什么被折叠?