課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
如何把刪除的子節(jié)點依次顯示出來?為何我的這個代碼每點擊兩次按鈕依次刪除一個子節(jié)點
2017-04-17
源自:JavaScript進階篇 9-14
正在回答
<!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"); ? ? ? ?// 在此完成該函數(shù) ? ? ? ?var conCN = content.childNodes; ? ? ? ?while ( (conCN.length > 0) && (conCN[0].nodeType != 1) ) ? ? ? ?{ ? ? ? ? ? ?content.removeChild(conCN[0]); ? ? ? ?} ? ? //回顯已刪除 ? ? ? var newDiv = document.createElement("div"); ? ?if (conCN.length >0) ? ?{ ? ? ? ?var rmNode = content.removeChild(conCN[0]); ? ?} ? ?else ? ?{ ? ? ? ?newDiv.innerHTML = "there is no node!" ? ?} ? ?content.parentNode.appendChild(newDiv); ? ?}</script><button onclick="clearText()">清除節(jié)點內容</button></body></html>
wfwy007 提問者
舉報
本課程從如何插入JS代碼開始,帶您進入網頁動態(tài)交互世界
1 回答按兩次才刪除一個子節(jié)點
1 回答js刪除子節(jié)點
2 回答如何實現(xiàn)逐個刪除節(jié)點?
5 回答想把HTML/CSS子節(jié)點都顯示出來,但是沒效果
3 回答想點擊一次刪除一個子節(jié)點,為什么必須點擊兩次才能刪除一個???
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-04-17
<!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");
? ? ? ?// 在此完成該函數(shù)
? ? ? ?var conCN = content.childNodes;
? ? ? ?while ( (conCN.length > 0) && (conCN[0].nodeType != 1) )
? ? ? ?{
? ? ? ? ? ?content.removeChild(conCN[0]);
? ? ? ?}
? ? //回顯已刪除
? ? ? var newDiv = document.createElement("div");
? ?if (conCN.length >0)
? ?{
? ? ? ?var rmNode = content.removeChild(conCN[0]);
? ?}
? ?else
? ?{
? ? ? ?newDiv.innerHTML = "there is no node!"
? ?}
? ?content.parentNode.appendChild(newDiv);
? ?}
</script>
<button onclick="clearText()">清除節(jié)點內容</button>
</body>
</html>