document.getElementById("bottom").style.top = 120px;js可以这样改变css的边距但是不可以  document.getElementById("bottom").style.marginTop = 120px;这样,怎么回事啊那遇到margin-top的时候怎么修改css的边距那?

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" > 
    <head> 
    <title>无标题页 </title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">
    window.onload = function(){
    document.getElementById('click').onclick = function(){
    var div = document.getElementById('div');
    div.style.marginTop = '1000px';

    alert(div.style.marginTop);
    };
    };
    </script>
    </head> 
    <body> 
    <div id="div" style="margin-top:10px;"></div> //把margin-top写在标签里
        <input id="click" type="button" onclick="aa()" value="click" />
    </body> 
    </html>
      

  2.   

    lz,你看下,可以的
    <div style="border:solid 1px #09f;" id="div1">
    <input type="button" id="btnSetStyle" value="设置上边距"/>
    </div>
    <script type="text/javascript">
    var $=function() {return document.getElementById(arguments[0]);}
    $("btnSetStyle").onclick=function() {
    $("div1").style.marginTop="130px";//lz可能你这里没加引号的原因
    }
    </script>
      

  3.   

    div.style.marginTop = "1000px";
    or
    div.style.marginTop = 1000;
      

  4.   

    为了兼容Firefox必须加px用引号
      

  5.   

    所有以 - 分隔的CSS样式在JS中都以这样的形式存在:a-b   ->  aB
      

  6.   

    对,楼主当遇到margin-Top,或者margin-left的时候要写成marginTop,或者marginLeft,等号后面的要有引号,或者1楼那样的形式也可以
      

  7.   

    说明一点,
    1,在JS中是区分大小写的;2,JS约定不同的词组合成关键JS字时首字母大写,除了开头的;比如getElementById(),marginTop等等3,值结果要加引号,就可以了
    document.getElementById("bottom").style.top = "120px";
    document.getElementById("bottom").style.marginTop = "120px";