纯javascript代码删除html文档的节点
例子:
复制代码 代码示例:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>javascript删除html文档的节点</title>
<script language="javascript">
function closeRecommend(obj){
obj.parentNode.parentNode.removeChild(obj.parentNode);
}
function clearText() {
var content=document.getElementById("content");
var len=content.childNodes.length;
for(var i=0;i<len;i++){
var childNode = content.childNodes[0];
content.removeChild(childNode);
}
}
</script>
</head>
<body>
<div id="content">
<h1>1</h1>
<h1>2</h1>
<h1>3</h1>
</div>
<button onclick="clearText();">清除节点内容</button>
<div id="content">
<h1>1</h1>
<h1>2</h1>
<h1>3</h1>
<button onclick="closeRecommend(this);">清除节点内容</button>
</div>
</body>
</html>