課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
為啥我這樣寫網(wǎng)頁炸了而且還刷新不了
2019-08-24
源自:JavaScript進階篇 9-14
正在回答
問題解決了,望采納
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</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");
document.write("content.innerHTML:<br>"+content.innerHTML);
document.write("content.childNodes[0].nodeValue="+content.childNodes[0].nodeValue+"<br>");
document.write("content.childNodes[1].nodeValue="+content.childNodes[1].nodeValue+"<br>");
document.write("content.childNodes[2].nodeValue="+content.childNodes[2].nodeValue+"<br>");
document.write("content.childNodes[0].nodeType="+content.childNodes[0].nodeType+"<br>");
document.write("content.childNodes[1].nodeType="+content.childNodes[1].nodeType+"<br>");
document.write("content.childNodes[2].nodeType="+content.childNodes[2].nodeType+"<br>");
document.write("content.childNodes[0].nodeName="+content.childNodes[0].nodeName+"<br>");
document.write("content.childNodes[1].nodeName="+content.childNodes[1].nodeName+"<br>");
document.write("content.childNodes[2].nodeName="+content.childNodes[2].nodeName+"<br><br><br>");
var otest = document.getElementsByTagName("h1");
document.write("content.childNodes.length="+content.childNodes.length+"<br>");
document.write("otest.length="+otest.length+"<br>");
var a = content.childNodes.length-1;
for(var i=a;i>=0;--i)
?
document.write("i="+i +"<br>");
document.write("content.childNodes[i].nodeName="+content.childNodes[i].nodeName +"<br>");
document.write("content.childNodes[i].nodeType="+content.childNodes[i].nodeType +"<br>");
while(content.childNodes[i].nodeType!="1")
--i;
}
? content.removeChild(content.childNodes[i]);
document.write(content.innerHTML);
? ? }
</script>
<button onclick="clearText()">清除節(jié)點內(nèi)容</button>
</body>
</html>
function clearText() {
? var content = document.getElementById("content");
// 1. 刪除該節(jié)點的內(nèi)容,先要獲取子節(jié)點。
var child = content.childNodes;
// 2. 然后使用循環(huán)遍歷每個子節(jié)點。
for (var i = child.length - 1; i >= 0; i--) {
// 3. 使用removeChild()刪除節(jié)點。
content.removeChild(child[i]);
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
18 回答我這樣寫網(wǎng)頁崩了………………
3 回答為什么我這樣寫頁面崩潰了
5 回答我這樣寫為什么執(zhí)行不了
3 回答這樣寫5還是動不了???
3 回答刷新了2次
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2019-08-27
問題解決了,望采納
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</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");
document.write("content.innerHTML:<br>"+content.innerHTML);
document.write("content.childNodes[0].nodeValue="+content.childNodes[0].nodeValue+"<br>");
document.write("content.childNodes[1].nodeValue="+content.childNodes[1].nodeValue+"<br>");
document.write("content.childNodes[2].nodeValue="+content.childNodes[2].nodeValue+"<br>");
document.write("content.childNodes[0].nodeType="+content.childNodes[0].nodeType+"<br>");
document.write("content.childNodes[1].nodeType="+content.childNodes[1].nodeType+"<br>");
document.write("content.childNodes[2].nodeType="+content.childNodes[2].nodeType+"<br>");
document.write("content.childNodes[0].nodeName="+content.childNodes[0].nodeName+"<br>");
document.write("content.childNodes[1].nodeName="+content.childNodes[1].nodeName+"<br>");
document.write("content.childNodes[2].nodeName="+content.childNodes[2].nodeName+"<br><br><br>");
var otest = document.getElementsByTagName("h1");
document.write("content.childNodes.length="+content.childNodes.length+"<br>");
document.write("otest.length="+otest.length+"<br>");
var a = content.childNodes.length-1;
for(var i=a;i>=0;--i)
?
{
document.write("i="+i +"<br>");
document.write("content.childNodes[i].nodeName="+content.childNodes[i].nodeName +"<br>");
document.write("content.childNodes[i].nodeType="+content.childNodes[i].nodeType +"<br>");
while(content.childNodes[i].nodeType!="1")
{
--i;
}
document.write("content.childNodes[i].nodeName="+content.childNodes[i].nodeName +"<br>");
document.write("content.childNodes[i].nodeType="+content.childNodes[i].nodeType +"<br>");
? content.removeChild(content.childNodes[i]);
document.write(content.innerHTML);
? ? }
}
</script>
<button onclick="clearText()">清除節(jié)點內(nèi)容</button>
</body>
</html>
2019-09-02
function clearText() {
? var content = document.getElementById("content");
// 1. 刪除該節(jié)點的內(nèi)容,先要獲取子節(jié)點。
var child = content.childNodes;
// 2. 然后使用循環(huán)遍歷每個子節(jié)點。
for (var i = child.length - 1; i >= 0; i--) {
// 3. 使用removeChild()刪除節(jié)點。
content.removeChild(child[i]);
}
}