`

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 integer Node type Constant
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_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 string Indicates
#comment This is a comment node.
#document This is the document node.
element.tagName The tagName of the element, indicating this is an element at the same time.
Attri.name The name of the attribute, indicating this is an attribute node at the same time.
#text This 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>

分享到:
评论

相关推荐

    Js nodeType 属性全面解析

    1. **Element节点**(类型编号1):这类节点表示HTML或XML文档中的元素标签,如`&lt;div&gt;`、`&lt;p&gt;`等。`nodeType`值为1时,表明这是一个元素节点。 2. **Attribute节点**(类型编号2):这类节点代表HTML或XML元素的...

    XML实例教程:nodeName、nodeValue和nodeType属性

    1. 元素节点(Element Node)的`nodeType`值为1,用于定义XML文档中的结构元素,如`&lt;book&gt;`。 2. 属性节点(Attribute Node)的`nodeType`值为2,表示元素的属性,如`&lt;book id="123"&gt;`中的`id`。 3. 文本节点(Text ...

    nodeType属性返回被选节点的节点类型介绍

    1. **Element节点** (节点编号1): `nodeType`值为1时,表示该节点是元素节点,如`&lt;div&gt;`、`&lt;p&gt;`等。元素节点是HTML或XML文档中最重要的部分,它们定义了页面结构。 2. **Attribute节点** (节点编号2): 当`nodeType`...

    js Element Traversal规范中的元素遍历方法.docx

    if (child.nodeType == 1) { processChild(child); } child = child.nextSibling; } ``` 但是,使用Element Traversal规范的属性,这个过程可以简化为: ```javascript var i, len, child = element....

    HTML DOM的nodeType值介绍

    nodeName、nodeValue 以及 nodeType 包含有关于节点的信息。nodeName 属性含有某个节点的名称。 元素节点的 nodeName 是标签名称 属性节点的 nodeName 是属性名称 文本节点的 nodeName 永远是 #text 文档节点的 ...

    JS.getTextContent(element,preformatted)使用介绍

    if (element.nodeType == 3 /*Node.TEXT_NODE*/) { var text = element.data; if (!preformatted) { //text = text.replace(/\n|\r|\t/g, ” “); text = normalizeNewlines(text); } return text; } if (ele

    isElement:检查参数是否为 HTML 元素

    `isElement`函数通常会检查一个变量是否具有`Node`接口中的`nodeType`属性,该属性表示节点的类型。在HTML元素节点中,`nodeType`的值应为1。因此,一个简单的`isElement`实现可能如下: ```javascript function is...

    JavaScript知识点个人总结

    1. `nodeType`:对于Element,其值为1。 2. `nodeName`:返回元素的标签名,与`tagName`相同。 3. `nodeValue`:对于Element,其值为null。 4. `parentNode`:可能是Document或另一个Element。 5. 子节点可以是...

    浅谈nodeName,nodeValue,nodeType,typeof 的区别

    1. 元素节点(Element):`nodeType`为1。 2. 属性节点(Attribute):`nodeType`为2。 3. 文本节点(Text):`nodeType`为3。 4. 注释节点(Comment):`nodeType`为8。 5. 文档节点(Document):`nodeType`为9。 ...

    js基础之DOM中元素对象的属性方法详解

    6. element.nodeType:返回节点类型,例如元素节点类型为1。 7. element.nodeValue:返回节点的值,对元素节点而言,这个值为null。 8. element.childNodes:返回一个包含元素所有子节点的NodeList对象。 9. element...

    解决ASP.NET AJAX在frame及iframe中跨域访问的问题

    if (element.self || element.nodeType === 9) return new Sys.UI.Point(0,0); var clientRect = element.getBoundingClientRect(); if (!clientRect) { return new Sys.UI.Point(0,0); } var ownerDocument =...

    js Element Traversal规范中的元素遍历方法

    if (child.nodeType == 1) { processChild(child); } child = child.nextSibling; } ``` 但是,使用Element Traversal规范,我们可以简化这个过程: ```javascript var i, len, child = element....

    浅析JavaScript中扫瞄器的兼容问题_.docx

    if (element.textContent) { return element.textContent; } else { return element.innerText; } ``` 2. 兄弟节点/元素的兼容性问题: - `nextSibling` 和 `previousSibling`:这两个属性在所有浏览器中都支持...

    javascript 节点

    console.log(elementNode.nodeType); // 输出:1 console.log(elementNode.nodeName); // 输出:"html" console.log(elementNode.nodeValue); // 输出:null // 获取文本节点 var textNode = document....

    JavaScript程序设计-操作DOM元素的节点及关系.pdf

    `element.nodeType` 属性返回一个整数值,指示节点的类型。常见的类型有: - 1 表示元素节点(Element) - 2 表示属性节点(Attr) - 3 表示文本节点(Text) - 8 表示注释节点(Comment) 例如,一个HTML...

Global site tag (gtag.js) - Google Analytics