每次我在 Internet Explorer 7 或更高版本中加载页面Object Expected
时,调用以下函数时都会出现错误。该脚本出现在页面底部的结束</body>
标记之前。没有具有相同name
or的元素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)
放置之前使用非 Internet Explorer 浏览器时出现的消息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>