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

解决方案 »

  1.   

    改为 window.onload = window.onresize = refresh_div; 即可
      

  2.   


    window.onload = window.onresize = refresh_div()
    改成
    window.onload = window.onresize = refresh_div
      

  3.   


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

  4.   

    恩 我改了以后在IE中可以, 但在FireFox中还是有问题。 不能看到绿色的div, 使用firebug设断点调试的时候会出现div 但是最后还是消失了。请大家帮忙测一下