<!DOCTYPE?HTML>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>無(wú)標(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");
??//?在此完成該函數(shù)
??var?a=content.childNodes;
??for(var?i=a.length-1;i>=0;i--){?????
????????var?x=content.removeChild(a[0]);????
??}
}?
</script>
<button?onclick="clearText()">清除節(jié)點(diǎn)內(nèi)容</button>
</body>
</html>
2015-09-01
function clearText() {
? var content=document.getElementById("content");
? //每單擊一次按鈕,依次從最后刪除一個(gè)nodechild,刪除break語(yǔ)句后可以一次全部清除節(jié)點(diǎn)。
?outerloop: ? ? ?//帶標(biāo)簽的代碼塊
? for(var i=(content.childNodes.length)-1; i>=0; i--){
? ? ?var lastnode = content.childNodes[i];
? ? if(lastnode.nodeType==1){? ? ??
? ? ? content.removeChild(lastnode);?
? ? ? break outerloop;//可以更精準(zhǔn)的指示流程控制
? ? } ?
? }
}
2015-08-28
function clearText() {
? var content=document.getElementById("content");
? var a=content.childNodes;
??? for(var i=a.length-1;i>=0;i--){????
????????? if(a[i].nodeType=='1'){//判斷元素類型為1則是h1,排除掉空格
?????????? content.removeChild(a[i]);
?????????? return false;//每執(zhí)行一次中斷循環(huán)
???? }?
?? }
}