javascript 无法读取 null 的属性“nodeValue”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10195429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot read property 'nodeValue' of null
提问by Nick
I have XML:
我有 XML:
<street></street>
or
或者
<street>2813 Bla ave</street>
Javascript:
Javascript:
if ((xmldoc.getElementsByTagName('street')[i].firstChild.nodeValue != null)) {
alert(1);
}
alert(2);
But the script doesn't work - Cannot read property 'nodeValue' of null
但脚本不起作用 - 无法读取 null 的属性“nodeValue”
回答by Niet the Dark Absol
nodeValue
will never be null
because without a value the node wouldn't exist.
nodeValue
永远不会,null
因为如果没有值,节点将不存在。
Remove .nodeValue
from your code.
.nodeValue
从您的代码中删除。
回答by Jlange
Your selector is failing,
你的选择器失败了,
xmldoc.getElementsByTagName('street')[i].firstChild
appears to return null. Have you tried logging and checking to make sure that the selector you want actually exists?
似乎返回空值。您是否尝试过记录和检查以确保您想要的选择器确实存在?
回答by Barlow Tucker
The street node does not have any children. You need to remove .firstChild
街道节点没有任何子节点。你需要删除.firstChild
回答by FlavorScape
Use xmldoc.getElementsByTagName('street')[i].innerHTML because the text you want is between the tags. I believe this IS supported for XML. Of course, you could always use the nodeValue property as well.
使用 xmldoc.getElementsByTagName('street')[i].innerHTML 因为您想要的文本位于标签之间。我相信这支持 XML。当然,您也可以始终使用 nodeValue 属性。