为什么我第一次点击按钮没有反应,当点击第二次的时候会同时清除三个节点。。。求解答。。
发条橙
2014-08-21
10 回答
for(var i=0;i<content.childNodes.length;i++)
content.childNodes.length值在变小,导致只循环3次
并且循环中
removeChild[0]不是removeChild[i]
上个代码,用了children,不会选择空文本,领会一下吧
<!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");
//console.log(content.childNodes.length);
// 在此完成该函数
var length = content.children.length;
//console.log(content.children.length);
for(var i=0;i<length;i++)
{
var x=content.removeChild(content.children[0]);
}
}
</script>
<button onclick="clearText()">清除节点内容</button>
</body>
</html>举报
0/150
提交
取消