js中xx.style.top、xx.style.left等取不到值的原因

  在js中有的朋友用xx.style.top取不到对象的值,很是郁闷。原因就在与style只能内嵌样式的值。 也就是说要这么写才能获取到值:

   <div id="xx" style="200px;height:200px; top:100px;"></div>

  下面是参考:

    var divTop =divObj.style.top   //style仅内嵌样式得到内嵌样式的属性值
    var divTop = divObj.currentStyle.top  //仅微软IE支持currentStyle,火狐谷歌不支持

    //getComputedStyle() --DOM提供的方法,火狐、谷歌、IE9支持
    var divTop = document.defaultView.getComputedStyle(divObj, null).top;  

其实也可以用:offsetLeft获取的 

   如:

    xx.offsetLeft;  //xx代表对象

详情请看我的另一篇随笔:http://www.cnblogs.com/clouds008/archive/2012/05/17/2506861.html ()

原文地址:https://www.cnblogs.com/clouds008/p/2506910.html