JavaScript:Internet Explorer中document.getElementById()出错
||
我目前在JavaScript中遇到错误。代码如下
function loader() {
size = window.innerWidth;
size = size - 300;
mainj = document.getElementById(\'main1\');
mainj.style.left = size.toString() + \'px\';
submainj = document.getElementById(\'submain1\');
submainj.style.left = size.toString() + \'px\';
size = mainj.style.top + 26;
document.getElementById(\'submain1\').style.top = size.toString() + \'px\';
}
onload = loader();
该错误仅在Internet Explorer中出现,并且代码在Firefox中可以完美运行。错误显示在第五行,错误是
Message: Object required
Line: 34
Char: 1
Code: 0
URI: http://localhost/home.php
第34行是代码中给出的第五行-\'mainj.style.left ..... \'
我该如何解决这个问题?
没有找到相关结果
已邀请:
2 个回复
弓萍功
据我所知,Internet Explorer在窗口上没有内部宽度的概念。以下代码是一个不错的跨浏览器实现,与您在库中看到的类似。如果还需要高度,则有相应的innerHeight和offsetHeight属性。
烷刨画颠离
的DOM元素 如果不存在,则您的代码将返回错误。 您可以做的是有条件的:
另外,请尝试在代码中使用局部变量(以“ 6”开头的变量),以免它们进入全局范围。