在JavaScript中,存在有nodeName 、nodeType、 nodeValue這三個屬性,今天我們來了解下JavaScript中的nodeName 、nodeType 、nodeValue區別
nodeName
nodeName 屬性含有某個節點的名稱。
* 元素節點的 nodeName 是標簽名稱
* 屬性節點的 nodeName 是屬性名稱
* 文本節點的 nodeName 永遠是 #text
* 文檔節點的 nodeName 永遠是 #document
注釋:nodeName 所包含的 XML 元素的標簽名稱永遠是大寫的.
nodeValue
對於文本節點,nodeValue 屬性包含文本。
對於屬性節點,nodeValue 屬性包含屬性值。
nodeValue 屬性對於文檔節點和元素節點是不可用的。
nodeType
nodeType 屬性可返回節點的類型。
最重要的節點類型是:
元素類型 節點類型
元素element 1
屬性attr 2
文本text 3
注釋comments 8
文檔document 9
當你打算去寫一個js的框架之類的時候,可能就會比較多的用到這幾個屬性的。這幾個屬性可以讓你拿到你所擁有的節點的屬性名稱,以及屬性名稱的內容,以及屬性和節點的類型。
初步大家可以看下這個網址里面的代碼演示。http://help.dottoro.com/ljiuhlep.php
下面是演示的代碼1:
<head><scripttype="text/javascript">function GetSpecifiedAttributes () { var message = "The following attributes specified for the body:\n"; for (var i=0; i < document.body.attributes.length; i++) { var attr = document.body.attributes[i]; if (attr.specified) { message += "\n" + attr.nodeName + "=" + attr.nodeValue; } } alert (message); } </script></head><bodyonload="GetSpecifiedAttributes ()"contentEditable="false"></body>