例如:<year>2005</year>的XML文件,取其中的2005.
现在使用下面的代码:
alert(node.nodeValue);
node的是指year节点。结果出现错误,node.nodeValue为null。
搜索W3C文档知道了原因:
原来XML中任何一个节点的值,都是一个Text元素!Text is Always Stored in Text Nodes
A common error in DOM processing is to expect an element node to contain text.
However, the text of an element node is stored in a text node.
In this example: <year>2005</year>, the element node <year>, holds a text node with the value "2005".
"2005" is not the value of the <year> element!
代码改为如下:
alert(node.firstChild.nodeValue);
就可以正常运行了!
没有评论:
发表评论