Element nodeType values

Last updated: Februrary 27th, 2006

The term "nodes" is just a fancy way of referring to all the elements in a document, whether it's a particular DIV, or the text contained inside it. The "nodeType" property of the DOM is very helpful in determining exactly the type of the node you're currently accessing, which isn't always so apparent. Here are the possible values returned by "nodeType":

nodeType values chart

Returned integerNode type Constant
1ELEMENT_NODE
2ATTRIBUTE_NODE
3TEXT_NODE
4CDATA_SECTION_NODE
5ENTITY_REFERENCE_NODE
6ENTITY_NODE
7PROCESSING_INSTRUCTION_NODE
8COMMENT_NODE
9DOCUMENT_NODE
10DOCUMENT_TYPE_NODE
11DOCUMENT_FRAGMENT_NODE
12`NOTATION_NODE

Consider the following HTML code:

<div id="adiv"><b>Some text</b></div>

<script type="text/javascript">
alert(document.getElementById("adiv").nodeType) //DIV element. Alerts 1
alert(document.getElementById("adiv").firstChild.nodeType) //B element. Alerts 1
</script>
 

 

With the above HTML block, you don't really need the "nodeType" property  to tell you the types of the three nodes you're accessing. But consider this slightly modified example:

<div id="adiv"> <b>Some text</b></div>

<script type="text/javascript">
alert(document.getElementById("adiv").nodeType) //DIV element. Alerts 1
alert(document.getElementById("adiv").firstChild.nodeType) //Alerts 1 or 3, depending on browser.
</script>
 

 

Here I've added a blank space in front of the B element. To some browsers such as Firefox, a blank space is considered a text node (nodeType=3) just like regular text, while in others such as IE, they are not. Due to this, "the next node" after the DIV element varies depending on which browser you ask, with Firefox saying it's a text node, while IE says it's an element node (B element). Without the help of the nodeType property when traversing the document, your script may very well lose its place.

nodeName property

If the integer value returned by the "nodeType" property is too abstract for you, a more human, albeit less robust way, of returning the type of a node is using the "nodeName" property. It returns a string indicating the name of the node. Returned value is in uppercase. Here are some common "nodeName" property values returned:

Returned stringIndicates
#commentThis is a comment node.
#documentThis is the document node.
element.tagNameThe tagName of the element, indicating this is an element at the same time.
Attri.nameThe name of the attribute, indicating this is an attribute node at the same time.
#textThis is a text node.

For example:

if (document.getElementById("test").firstChild.nodeName=="DIV")
alert("This is a DIV")
 

 

nodeValue property

The "nodeValue" property is a read/write property that reflects the current value of a node. For text nodes, the content of the node is returned, while for attribute nodes, the attribute value. Null is returned for Document and element nodes. Use this property to alter the contents of a text or attribute node.

<div id="test">Old text</div>

<script type="text/javascript">
if (document.getElementById("test").firstChild.nodeName=="#text")
document.getElementById("test").firstChild.nodeValue="New text"

</script>
 

 

<script type="text/javascript"></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script> <script type="text/javascript"></script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值