不懂這里是什么意思 if (content.childNodes[i].nodeType!=1){ continue; }
<!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");
? ? ? ?for (var i=0;i<content.childNodes.length;i++){
? ? ? ? ? ?if (content.childNodes[i].nodeType!=1){
? ? ? ? ? ? ? ?continue;
? ? ? ? ? ?}
? ? ? ? ? ?else {
? ? ? ? ? ? ? ?content.removeChild(content.childNodes[i]);
? ? ? ? ? ?}
? ? ? ?}
? ?}
</script>
<button onclick="clearText()">清除節點內容</button>
</body>
</html>
2016-09-13
在DOM樹里面 每一個節點都是有一個特殊的值進行標注的。
表示為的節點屬性為nodeType。
其中只有nodeType值為1的時候才代表這是一個元素節點(ElementNode)。
2016-09-13
在非IE瀏覽器中,任意一個空格,一個換行都會被認為一個節點,所以需要這種判斷來過濾這種非元素的DOM節點