不需要用JS
定义一个margin:auto;就行了
比如
<div style="margin:auto;width:100px;height:100px;backgroud-color:blue;"></div>

解决方案 »

  1.   

    比如163邮箱的删除邮件提示框,上下左右都居中。在存在 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">的情况下该如何做到?
      

  2.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【shaoyk】截止到2008-06-26 22:16:44的历史汇总数据(不包括此帖):
    发帖数:2                  发帖分:40                 
    结贴数:1                  结贴分:20                 
    未结数:1                  未结分:20                 
    结贴率:50.00 %            结分率:50.00 %            
    楼主加油
      

  3.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    </head>
    <body>
    <div id="div1" style="background-color:blue;width:300px;height:100px;position:absolute;"></div>
    </body>
    <script>
    var theDiv = document.getElementById("div1");theDiv.style.left = (document.documentElement.offsetWidth-theDiv.offsetWidth)/2+"px";
    theDiv.style.top = (document.documentElement.offsetHeight-theDiv.offsetHeight)/2+"px";
    </script>
    </html>
      

  4.   

    谢谢楼上朋友的回答。
    按照您给的代码,我加了几行代码,如下:可还是无法显示在当前网页的可视部分的正中。
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    </head>
    <body>
        <div id="div1" style="background-color:blue;width:300px;height:100px;position:absolute;"></div>
    <div style="height:2000px;"></div><input type=button value=click onclick='s();'/>
    </body>
    <script>
    function s(){
    var theDiv = document.getElementById("div1");theDiv.style.left = (document.documentElement.offsetWidth-theDiv.offsetWidth)/2+"px";
    theDiv.style.top = (document.documentElement.offsetHeight-theDiv.offsetHeight)/2+"px";
    }
    </script>
    </html>