JavaScript删除DOM节点

 
更多

使用JavaScript原生态的语法删除html文档的节点,具体操作方法见下面两个案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</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>

 

打赏

本文固定链接: https://www.cxy163.net/archives/4597 | 绝缘体

该日志由 绝缘体.. 于 2013年05月23日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: JavaScript删除DOM节点 | 绝缘体
关键字: , , , ,

JavaScript删除DOM节点:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter