这里有点奇怪的故障。
当 IF 语句被注释掉时, a 的标签名称childNode
被正确记录,但是一旦你取消 IF 语句的注释,那么childNode
s 标签名称就会被记录为“ #TEXT
”。
<body>
<div id="page_Wrapper">
<h1>h1</h1>
<br />
<br />
<br />
<b>b</b>
<br />
<i>i</i>
<br />
<u>u</u>
<div>div</div>
<span>span</span>
</div>
<script>
window.onLoad = removeTags();
function removeTags(){
var allTags = document.getElementById("page_Wrapper").childNodes;
console.log("allTags length: " + allTags.length);
for (var i = 0; i < allTags.length; i++){
var tag = allTags[i].nodeName.toLowerCase();
console.log(tag);
/*
if (tag != "b" || tag != "br" || tag != "i"){
allTags[i].parentNode.removeChild(allTags[i]);
}
*/
}
}
</script>
</body>
http://jsfiddle.net/MythOfEchelon/VpNWA/3/
完成,工作代码:
<body>
<div id="page_Wrapper">
<h1>h1</h1>
<br />
<br />
<br />
<b>b</b>
<br />
<i>i</i>
<br />
<u>u</u>
<div>div</div>
<span>span</span>
</div>
<script>
window.onload = removeTags;
function removeTags(){
var allTags = document.getElementById("page_Wrapper").childNodes;
var i = allTags.length;
while (i--){
var tag = allTags[i].nodeName.toLowerCase();
console.log(tag);
if (tag != "b" && tag != "br" && tag != "i"){
allTags[i].parentNode.removeChild(allTags[i]);
}
}
}
</script>
</body>