最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'nodeValue' of null - Stack Overflow

programmeradmin3浏览0评论

I have XML:

<street></street>

or

<street>2813 Bla ave</street>

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

I have XML:

<street></street>

or

<street>2813 Bla ave</street>

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

Share Improve this question asked Apr 17, 2012 at 16:44 NickNick 1,5702 gold badges16 silver badges23 bronze badges 3
  • You should give more details, like the full XML and the full code! – gdoron Commented Apr 17, 2012 at 16:45
  • .firstChild is null, so - xmldoc.getElementsByTagName('street')[i] has no children. – c69 Commented Apr 17, 2012 at 16:46
  • @Kolink I'm assuming his javascript resides in some sort of loop. – RestingRobot Commented Apr 17, 2012 at 16:48
Add a ment  | 

4 Answers 4

Reset to default 3

nodeValue will never be null because without a value the node wouldn't exist.

Remove .nodeValue from your code.

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?

The street node does not have any children. You need to remove .firstChild

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.

发布评论

评论列表(0)

  1. 暂无评论