简体   繁体   中英

Javascript snippet not working in Chrome, but works in Internet Explorer

I have a really basic snippet of javascript code that works in Internet Explorer, but not Google Chrome. Here's the line it breaks on:

var formChildren = document.getElementById('myForm').children;

This is the first line in my script. The error I receive is:

cannot read property 'children' of null.

Does anyone know if I'm missing something? I can't imagine why Chrome would error out on such a basic line.

This is part of an MVC 4 project if that makes any difference.

Thank you in advance!

Chrome actually does return null in an instance where it is unable to find an element with that ID. You could hide this error by using (document.getElementById('myForm') || {}).children

My problem is solved. Yes I did some extra testing and realized that I was able to use getElementsByName successfully on both IE and Chrome so I took a look and realized I forgot to add the Id to the form. It was still unexpected that IE knew what it element was without the id set, but the problem is solved.

Thank you Kyle and hackNightly, your input helped a lot in narrowing down my answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM