名词“节点”其实就是关联文档内部所有元素的一个东东,不管它是特定的DIV标记,还是包含的文本,我们都叫这个东东为节点;而它的“节点类型”属性能帮助你准确地判断当前正在访问的节点是什么类型。(这不废话么,En国人也真是的)。下面是“节点类型”可能的值:
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":
[img]http://www.iteye.com/upload/attachment/30859/ad3c7c6d-fdbf-3a09-b7a2-9e878e211cc1.png[/img]
看看下面的代码:
Consider the following HTML code:
看起来哈,如果使用HTML的语句块的话,或许并不真的需要使用“节点类型”这个属性来告诉自己节点的类型。(自己使用什么标记,自己当然晓得了)。那再看看下面稍稍修改的代码:
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:
(你发现差别了么,我开始时就没注意!)
在上面的代码中,我放了个空格在<B>元素前面。对一些浏览器来说(比如FF), 一个空格会被认为是文本节点(nodeType=3), 但是IE就不这样认为,所以究竟结果如何是与你的浏览器有关的。(所以有时使用“节点类型”这个属性判断一下还是有用的。)
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.
[b]节点名称 nodeName property[/b]
“节点类型”返回的是一个数字,对大家来说或许太抽象了,所以可以使用“节点名称”这个属性,尽管它不够强壮,但它更人性化,更直观。 “节点名称”属性返回的是一个标识节点的字符串。下面是一些常见的返回的“节点名称”:
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:
[img]http://www.iteye.com/upload/attachment/30857/be3bf969-26ec-3498-ae6c-3c2549302303.png[/img]
For example:
[b]nodeValue property[/b]
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.
原文:http://www.javascriptkit.com/domref/nodetype.shtml
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":
[img]http://www.iteye.com/upload/attachment/30859/ad3c7c6d-fdbf-3a09-b7a2-9e878e211cc1.png[/img]
看看下面的代码:
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>
看起来哈,如果使用HTML的语句块的话,或许并不真的需要使用“节点类型”这个属性来告诉自己节点的类型。(自己使用什么标记,自己当然晓得了)。那再看看下面稍稍修改的代码:
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>
(你发现差别了么,我开始时就没注意!)
在上面的代码中,我放了个空格在<B>元素前面。对一些浏览器来说(比如FF), 一个空格会被认为是文本节点(nodeType=3), 但是IE就不这样认为,所以究竟结果如何是与你的浏览器有关的。(所以有时使用“节点类型”这个属性判断一下还是有用的。)
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.
[b]节点名称 nodeName property[/b]
“节点类型”返回的是一个数字,对大家来说或许太抽象了,所以可以使用“节点名称”这个属性,尽管它不够强壮,但它更人性化,更直观。 “节点名称”属性返回的是一个标识节点的字符串。下面是一些常见的返回的“节点名称”:
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:
[img]http://www.iteye.com/upload/attachment/30857/be3bf969-26ec-3498-ae6c-3c2549302303.png[/img]
For example:
if (document.getElementById("test").firstChild.nodeName=="DIV")
alert("This is a DIV")
[b]nodeValue property[/b]
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>
原文:http://www.javascriptkit.com/domref/nodetype.shtml