我這段代碼為什么清除的時候還要點多次?按理說點一次按鈕,for循環就開始到結束了,為什么要點多次?
<!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");
? // 在此完成該函數
? for(i=0;i<content.childNodes.length;i++){
? ? content.removeChild(content.childNodes[i]);
? ? // ? document.write("被刪除的內容:"+c);
? }
??
}
</script>
<button onclick="clearText()">清除節點內容</button>
</body>
</html>
2018-09-12
循環的時候從后往前刪,就能點一次全部刪除
function clearText() {
? var content=document.getElementById("content");
? // 在此完成該函數
? var nodes=content.childNodes;
? for(var i=nodes.length-1;i>=0;i--){
? ? ??
? ? ? content.removeChild(nodes[i])
? ? ??
? }
??
??
}
2018-11-09
for外面套while循環就行
2018-09-26
你用for循環,如果你不是i--往后面刪除的話,是i++這個方法的話,當i=0刪除的是html,再循環一次,這時刪除的就是javascript了,因為html不在了,所以i=1? ,javascript的下標就是1了
2018-09-12
循環的時候從后往前刪,就能點一次全部刪除
function clearText() {
? var content=document.getElementById("content");
? // 在此完成該函數
? var nodes=content.childNodes;
? for(var i=nodes.length-1;i>=0;i--){
? ? ??
? ? ? content.removeChild(nodes[i])
? ? ??
? }
??
??
}
2018-08-25
因為每for循環一次,數組的下標就會發生改變,i= 0時,刪除content[0],html被刪,此時content[1]時JavaScript,而不是php