呢绒
2016-07-22 14:42
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<div id="content">
<h1>html</h1>
<h1>php</h1>
<h1>javascript</h1>
<h1>jquery</h1>
<h1>java</h1>
</div>
<script type="text/javascript">
function clearText()
{
// var content=document.getElementById("content");
var hs=document.getElementsByTagName("h1");
for(var i=0;i<hs.length;i++)
{
var a=hs.childNodes[i];
var b=hs.removeChild(a);
document.write("删除h1节点的内容为"+b[i].innerHTML);
}
}
</script>
<button onclick="clearText()">清除节点内容</button>
</body>
</html>
要删除hs节点必须先得到他的父节点,然后removeChild(),才可以删除
function clearText()
{
var content=document.getElementById("content");
var hs=document.getElementsByTagName("h1");
for(var i=0;i<hs.length;i++)
{
content.removeChild(hs[i])
// var a=hs.childNodes[i];
// var b=hs.removeChild(a);
// document.write("删除h1节点的内容为"+b[i].innerHTML);
}
}
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题