document.getElementById() returns null on IE9

*爱你&永不变心* 提交于 2019-12-01 15:01:03

问题


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:

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?

Note: The function is called in the body's onLoad atribute of my html.


回答1:


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';
 }



回答2:


IE is having some known issues with getElementById.This post may help .

  1. http://webbugtrack.blogspot.com/2007/08/bug-152-getelementbyid-returns.html

  2. http://www.impressivewebs.com/avoiding-problems-with-javascript-getelementbyid-method-in-internet-explorer-7/




回答3:


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.

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.



来源:https://stackoverflow.com/questions/8981337/document-getelementbyid-returns-null-on-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!