我有一个非常基本的 JavaScript 代码片段,可以在 Internet Explorer 中运行,但不能在 Google Chrome 中运行。这是它中断的行:
var formChildren = document.getElementById('myForm').children;
这是我脚本中的第一行。我收到的错误是:
cannot read property 'children' of null.
有人知道我是否遗漏了什么吗?我无法想象为什么 Chrome 会在这么基本的一行上出错。
如果这有什么不同的话,这是 MVC 4 项目的一部分。
提前谢谢您!
最佳答案
Chrome 在无法找到具有该 ID 的元素的情况下实际上会返回 null
。您可以使用 (document.getElementById('myForm') || {}).children
关于Javascript 代码片段无法在 Chrome 中运行,但可以在 Internet Explorer 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20480254/