為什么我的按鈕點擊后沒有清除內容
<!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(var i=o;i<content.childNodes.length;i++){
????? var x=document.removeChild(content.childNodes[i]);
? }
?
}
</script>
<button onclick="clearText()">清除節點內容</button>
</body>
</html>
2018-05-09
但我這樣寫是哪里錯了呀?
2018-05-09
使用for(var i=o;i<content.childNodes.length;i++)的話,每次循環后length都在減小,可以定義在外面:
? var len = content.childNodes.length;
? for(var i=o;i<len;i++){
????? var x=document.removeChild(content.childNodes[i]);
? }