`

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 属性全面解析

    语法:elementNode.nodeType 节点编号: 节点名称: 1 Element 2 Attribute 3 Text 4 CDATA Section 5 Entity Reference 6 Entity 7 Processing Instrucion 8 Comment 9 Document 10 Document Type 11...

    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

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

    本文详细介绍了nodeName、nodeValue和nodeType属性

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

    主要介绍了nodeType属性返回被选节点的节点类型。需要的朋友可以过来参考下,希望对大家有所帮助

    关于firefox的ElementTraversal 接口 使用说明

    firstElementChild 访问一元素的这个属性必须将该nodeType 1 元素第一子节点引用返回为 Element 对象。如果该被访问元素属性没有任何子节点,或者如果所有这些子节点都不是元素节点, 那么该属性必须返回 null。 ...

    解决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 =...

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

    nodeName 属性含有某个节点的名称。 元素节点的 nodeName 是标签名称 属性节点的 nodeName 是属性名称 文本节点的 nodeName 永远是 #text ...元素element1属性attr2文本text3注释comments8文档document9补充: 值-元素

    Neos.NodeType.CodeMirror:允许在 TYPO3 Neos 中使用 codeMirror 样式代码片段的节点类型

    Neos.NodeType.CodeMirror 允许在 TYPO3 Neos 中使用 codeMirror 样式代码片段的节点类型将此添加到您的 composer.json 以启用代码片段节点类型: { "repositories": [ { "type": "git", "url": ...

    openstack:用于与 OpenStack IaaS 交互的 TOSCA NodeType 和 ImplementationArtifact

    开放式堆栈用于与 OpenStack IaaS 交互的 TOSCA NodeType 和 ImplementationArtifact输入所有网络方法至少需要两个输入: 证书端点API 它们的语法必须是: 凭据:{"auth":{"tenantId":"???","passwordCredentials":{...

    lmnt:元素遍历 polyfill

    这些属性直接对应 interface 定义的,附加的约束是它们只操作 nodeType 1(即 Element 节点),忽略 Text、Comment 和其他 nodeType。 十有八九,这正是 Web 开发人员想要做的事情,因此使用这些将。 polyfill 还将...

    nodetypes-contentreferences:[只读] Contentreferences NodeType

    Neos NodeType:ContentReferences 该程序包实现节点类型ContentReferences。 它显示引用的节点。 这最初是Neos.NodeTypes包的一部分。 贡献 如果您想为Neos做出贡献,请查看它是用于开发的存储库,所有请求请求都...

    nodetypes-html:[只读] HTML NodeType

    Neos NodeType:HTML 该程序包实现了一个节点类型Html以显示任意html代码。 这最初是Neos.NodeTypes包的一部分。 贡献 如果您想为Neos做出贡献,请查看它是用于开发的存储库,所有请求请求都应包含在其中。

    DOMhelp.js

    while(tempObj.nodeType != 1 && tempObj.previousSibling != null){ tempObj = tempObj.previousSibling; } return (tempObj.nodeType == 1)?tempObj:false; }, //Find the first sibling of the current ...

    如何判断元素是否为HTMLElement元素

    我们经常使用nodeType==1判断元素是否是一个HMTLElement元素。页面上的元素都是节点(Node),有元素节点(Element Node)、属性节点(Attribute Node)、文本节点(Text Node)等。w3c nodeType 的定义如下 const unsigned ...

    精通JavaScript 纠正 cleanWhitespace函数

    代码如下: function cleanWhitespace(element){ //如果不提供参数,则...= null){ //保存当前节点的下个节点 tmp=cur.nextSibling //如果节点为文本节点,应且包含空格 if ( cur.nodeType == 3 && ! /\S/.test(cur.n

Global site tag (gtag.js) - Google Analytics