我的 document.getElementById();在firefox下老是找不到对象 返回null。下面使我的代码, 请各位给我看一下。代码我只在firfox中测试过,我想IE是没问题的。主要效果就是 div总是在浏览器的中间<html>
<head>
<title>onresize.html</title>
<style type="text/css">
*{
font-size:12px;
font-family:宋体, Arial;
font-weight:normal;
color:#333;
}
#greenDiv{
width:240px;
height:16px;
border:2px solid #00DD00;
background-color:#BBF0BB;
}
</style>
<script type="text/javascript">
function refresh_div(){ var div_width = 240, div_height = 160;
document.getElementById("div").style.position = "absolute";
$("div").style.left = (document.body.offsetWidth - div_width)/2;
$("div").style.top = (document.body.offsetHeight - div_height)/2;
} function $(str){
return document.getElementById(str);
} window.onload = window.onresize = refresh_div();
</script>
</head>
<body>
<div id="div" name="div"></div>
</body>
</html>
<head>
<title>onresize.html</title>
<style type="text/css">
*{
font-size:12px;
font-family:宋体, Arial;
font-weight:normal;
color:#333;
}
#greenDiv{
width:240px;
height:16px;
border:2px solid #00DD00;
background-color:#BBF0BB;
}
</style>
<script type="text/javascript">
function refresh_div(){ var div_width = 240, div_height = 160;
document.getElementById("div").style.position = "absolute";
$("div").style.left = (document.body.offsetWidth - div_width)/2;
$("div").style.top = (document.body.offsetHeight - div_height)/2;
} function $(str){
return document.getElementById(str);
} window.onload = window.onresize = refresh_div();
</script>
</head>
<body>
<div id="div" name="div"></div>
</body>
</html>
window.onload = window.onresize = refresh_div()
改成
window.onload = window.onresize = refresh_div
<html>
<head>
<title>onresize.html</title>
<style type="text/css">
*{
font-size:12px;
font-family:宋体, Arial;
font-weight:normal;
color:#333;
}
#greenDiv{
width:240px;
height:160px;
border:2px solid #00DD00;
background-color:#BBF0BB;
}
</style>
<script type="text/javascript">
function refresh_div(){ var div_width = 240, div_height = 160;
document.getElementById("greenDiv").style.position = "absolute";
$("greenDiv").style.left = (document.body.clientWidth - div_width)/2;
$("greenDiv").style.top = (document.body.clientHeight - div_height)/2;
} function $(str){
return document.getElementById(str);
} window.onload = window.onresize = refresh_div;
</script>
</head>
<body>
<div id="greenDiv"></div>
</body>
</html>