課程
/前端開(kāi)發(fā)
/JavaScript
/JavaScript進(jìn)階篇
我用的360,不是應(yīng)該算文字空白節(jié)點(diǎn)嗎?我試了下,childNodes的長(zhǎng)度是11啊,那應(yīng)該是刪除13579???怎么會(huì)是連續(xù)的?
2017-01-13
源自:JavaScript進(jìn)階篇 9-14
正在回答
? var content=document.getElementById("content");
? // 在此完成該函數(shù)
? document.write(content.childNodes.length);
? for(var i=0;i<content.childNodes.length;i++)
? {
? ? ? if(content.childNodes[i].nodeType!=3)
? ? ? {
? ? ? ? ? var x=content.removeChild(content.childNodes[i]);
? ? ? ? ? document.write("刪除"+i+":"+x.innerHTML+"<br/>");
? ? ? ? ??
? ? ? }
document.write(content.childNodes.length);?
? }
}
在你的代碼中添加了兩個(gè)document.write(content.childNodes.length); ?當(dāng)for循環(huán)執(zhí)行第一次刪除后,節(jié)點(diǎn)變成了10個(gè),也就是說(shuō)子節(jié)點(diǎn)數(shù)組中 node[0] node[1] 都是空白節(jié)點(diǎn)了,node[2] 變成了<h1>php</h1>,然后繼續(xù)循環(huán),只要?jiǎng)h除一次節(jié)點(diǎn) 數(shù)組長(zhǎng)度就減1.然后從新計(jì)算順序。不知道你明白不?
舉報(bào)
本課程從如何插入JS代碼開(kāi)始,帶您進(jìn)入網(wǎng)頁(yè)動(dòng)態(tài)交互世界
2 回答如何獲取要?jiǎng)h除行的序號(hào)?
1 回答運(yùn)行結(jié)果是什么
1 回答為什么運(yùn)行結(jié)果都是undefined
1 回答為什么運(yùn)行結(jié)果都是NaN
1 回答為什么運(yùn)行沒(méi)結(jié)果。。。。
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2017-01-18
? var content=document.getElementById("content");
? // 在此完成該函數(shù)
? document.write(content.childNodes.length);
? for(var i=0;i<content.childNodes.length;i++)
? {
? ? ? if(content.childNodes[i].nodeType!=3)
? ? ? {
? ? ? ? ? var x=content.removeChild(content.childNodes[i]);
? ? ? ? ? document.write("刪除"+i+":"+x.innerHTML+"<br/>");
? ? ? ? ??
? ? ? }
document.write(content.childNodes.length);?
? }
}
在你的代碼中添加了兩個(gè)document.write(content.childNodes.length); ?當(dāng)for循環(huán)執(zhí)行第一次刪除后,節(jié)點(diǎn)變成了10個(gè),也就是說(shuō)子節(jié)點(diǎn)數(shù)組中 node[0] node[1] 都是空白節(jié)點(diǎn)了,node[2] 變成了<h1>php</h1>,然后繼續(xù)循環(huán),只要?jiǎng)h除一次節(jié)點(diǎn) 數(shù)組長(zhǎng)度就減1.然后從新計(jì)算順序。不知道你明白不?