每次我在 Internet Explorer 7 或更高版本中加载页面时,都会收到错误 Object Expected
当调用下面的函数时。该脚本在结束之前出现在页面底部 </body>
标签。没有具有相同 name
的元素或id
.
<script type="text/javascript">
window.onload = show();
</script>
它尝试调用的 Javascript 函数是
function show() {
obj1
= document.getElementById("container").innerHTML
= '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p>'
+ '<p><a href="#" onclick="hide();">test</a></p></div>';
}
- 为什么此错误不会出现在任何其他浏览器中?
- 我需要更改哪些内容才能解决该问题?
编辑 0
如果我移动函数show
进入同一个区 block window.onload
, hide()
现在不再有效。
Javascript代码
function show() {
obj1
= document.getElementById("container").innerHTML
= '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p>'
+ '<p><a href="#" onclick="hide();">test</a></p></div>';
}
function hide() {
obj1 = document.getElementById("container");
if(obj1){
alert("Hi");
obj1.style.display = "none";
obj1.style.visibility = "hidden";
}else{
alert("Cannot find the element with id container.");
}
}
HTML 代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict/EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtm1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset="UTF-8" />
<meta http-equiv="content-language" content="en-us" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<title>takeover</title>
<base href="" />
<link rel="stylesheet" href="" />
<style type="text/css" media="all" />
#container {
position:absolute;
text-align:center;
background-color:#fff;
z-index:9999;
}
</style>
<script type="text/javascript" src="takeover.js"></script>
</head>
<body>
<div>
<div id="container"></div>
<p><a href="#">qwe</a></p>
</div>
<script type="text/javascript">
window.onload = show;
</script>
</body>
</html>
编辑 1
使用非 Internet Explorer 浏览器放置 alert(show)
时出现的消息之前window.onload
编辑2
删除所有空格后显示的消息。同样,这仅适用于非 Internet Explorer 浏览器。
编辑3
尝试了 window.show = function show() 和 window.hide = function hide() 但在 Internet Explorer 中仍然出现错误。错误如下所示。
编辑4
这是更新后的代码,所有函数都在一个文件中。这在任何其他浏览器中都不起作用,我收到错误 show
未定义。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict/EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtm1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset="UTF-8" />
<meta http-equiv="content-language" content="en-us" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<title>takeover</title>
<base href="" />
<link rel="stylesheet" href="" />
<style type="text/css" media="all" />
#container {
position:absolute;
text-align:center;
background-color:#fff;
z-index:9999;
}
</style>
</head>
<body>
<div>
<div id="container"></div>
<p><a href="#">qwe</a></p>
</div>
<script type="text/javascript">
alert(show)
window.show = function show() {
obj1 = document.getElementById("container").innerHTML = '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p><p><a href="#" onclick="hide();">test</a></p></div>';
}
window.hide = function hide() {
obj1 = document.getElementById("container");
if(obj1)
{
alert("Hi");
obj1.style.display = "none";
obj1.style.visibility = "hidden";
}
else
{
alert("Cannot find the element with id container.");
}
}
window.onload = window.show;
</script>
</body>
</html>
最佳答案
这一行...
window.onload = show();
应该是这个...
window.onload = show;
...因为您需要将 show
函数本身分配给 window.onload
,而不是调用它的返回值。
关于javascript - Internet Explorer 7+ 返回错误 "Object expected",但其他浏览器都不会返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11007242/