每次在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
移到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
在alert(show)
window.onload
时使用非Internet Explorer浏览器时显示的消息
编辑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>
答案 0 :(得分:5)
这一行...
window.onload = show();
应该是......
window.onload = show;
...因为你需要将show
函数本身分配给window.onload
,而不是调用它的返回值。
答案 1 :(得分:0)
在 <script>
定义后,将引用container
的{{1}}块移动到点
container
例如,如果<!DOCTYPE HTML>
<HTML>
<HEAD>
</HEAD>
<BODY>
<DIV id="container"></DIV>
<script type="text/javascript">
window.onload = 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>';
}
</script>
</BODY>
</HTML>
块位于<script>
内,则Internet Explorer会出错:
页面加载SCRIPT5007:无法设置属性'innerHTML'的值:object为null或undefined
。