javascript document.getElementById() 在 IE9 上返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8981337/
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
document.getElementById() returns null on IE9
提问by Francisco Ragout
I'm working on a popup and i'm having some hard time with Internet Explorer 9. This is the piece of code that gives me trouble:
我正在处理一个弹出窗口,但我在 Internet Explorer 9 上遇到了一些困难。这是给我带来麻烦的一段代码:
var popUp= document.getElementById('projectInfo');
popUp.style.left=(tempX-310)+'px';
popUp.style.top=(tempY-110)+'px';
In IE9 (haven't tested in prior versions), popup is null. In adition, i've tried including my .js file just before the body closing tag and wrapping my function in a "document.ready()" function but none of this worked. The same code though works perfectly in Opera, Chrome and Firefox. Does anyone know what's going on?
在 IE9 中(在之前的版本中没有测试过),popup 是空的。此外,我尝试在正文结束标记之前包含我的 .js 文件并将我的函数包装在“document.ready()”函数中,但这些都不起作用。相同的代码虽然在 Opera、Chrome 和 Firefox 中完美运行。有谁知道发生了什么?
Note: The function is called in the body's onLoad atribute of my html.
注意:该函数是在我的 html 的主体的 onLoad 属性中调用的。
回答by azarudeen ajees
Without using function it can't work
不使用函数就无法工作
window.onload = function() {
var popUp= document.getElementById('projectInfo');
popUp.style.left=(tempX-310)+'px';
popUp.style.top=(tempY-110)+'px';
}
回答by Anderson
In previous versions of IE (and apparently Chrome and Firefox), getElementById would check for an object with the given id and if it didn't find it, it would take an element with that name.
在以前版本的 IE(显然还有 Chrome 和 Firefox)中,getElementById 会检查具有给定 id 的对象,如果没有找到,它将采用具有该名称的元素。
IE9 doesn't do this, so you need to make sure you have an element with id = projectInfo, not just name=projectInfo. We just discovered this throughout one of our applications. Not great.
IE9 不会这样做,因此您需要确保您有一个 id = projectInfo 的元素,而不仅仅是 name=projectInfo。我们刚刚在我们的一个应用程序中发现了这一点。不是很好。
回答by Ajeet Sinha
IE is having some known issues with getElementById.This post may help .
IE 的 getElementById 存在一些已知问题。这篇文章可能会有所帮助。