Firfox 中javaScript 问题

linuxlsx 2009-06-26 11:04:30
我的 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>
...全文
52 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
linuxlsx 2009-06-26
  • 打赏
  • 举报
回复
恩 我改了以后在IE中可以, 但在FireFox中还是有问题。 不能看到绿色的div, 使用firebug设断点调试的时候会出现div 但是最后还是消失了。请大家帮忙测一下
mqc507 2009-06-26
  • 打赏
  • 举报
回复

<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>

eugenepada 2009-06-26
  • 打赏
  • 举报
回复

window.onload = window.onresize = refresh_div()


改成


window.onload = window.onresize = refresh_div
monexus 2009-06-26
  • 打赏
  • 举报
回复
改为 window.onload = window.onresize = refresh_div; 即可
linuxlsx 2009-06-26
  • 打赏
  • 举报
回复
自己定一下

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧