<body>
  <div id="odiv" style="margin-left:30px;">ddddd</div>
  <script type="text/javascript">
  <!--
alert(document.getElementById("odiv").style.marginLeft)
  //-->
  </script>
 </body>

解决方案 »

  1.   

    不错,不过
    <style>
    odiv {margin:30px;}
    </style><body>
      <div id="odiv" >ddddd</div>
      <script type="text/javascript">
      <!--
        alert(document.getElementById("odiv").style.marginLeft)
      //-->
      </script>
     </body>就没有的了
      

  2.   

    <style>
    #odiv {margin:30px;}
    </style><body>
      <div id="odiv" >ddddd </div>
      <script type="text/javascript">
      <!--
      function getCurrentStyle(element) {
        return element.currentStyle || document.defaultView &&
          document.defaultView.getComputedStyle(element, null);
      }
      alert(getCurrentStyle(document.getElementById("odiv")).marginLeft)
      //-->
      </script>
    </body> 
      

  3.   

    如果没有设置的 margin的值是取不到的,只有设置了才能取到,要不然就会空白
      

  4.   

    在JS中要想兼容的情况下,style属性只能读取嵌入在HTML代码中的样式。
    对于其它样式,无论内部(写在style标签之间)还是外部(导入的样式表)都读取不到。
      

  5.   

    经过测试发现
      return element.currentStyle 是IE的东西
    document.defaultView && document.defaultView.getComputedStyle(element, null);是Firefox的东西。
    但是具体什么意思不太清楚。
      

  6.   

    element.currentStyle和document.defaultView.getComputedStyle(element, null)得到的是当前元素计算后的最终样式结果集
    element.style是由对元素设置了或元素的属性理设置了才会有