javascript get element width, height

Sorry, this article is not written in English and has not been translated into English yet .You can view this article in English with Google Translate, or please come back later.

在写一个javascript截图功能的时候,想获取一个没有定义css的div的宽度,于是使用了

var a=doucment.getElementById('dom').style.width

结果惊奇的发现获取到的值是undefined... WHY??

尝试了offsetWidth结果发现不是自己想要的值,突然想到曾经看到过computerStyle之类的东西,于是百度之,终于知道为什么了。

style                  //只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的。
currentStyle         //可以弥补style的不足,但是只适用于IE。
getComputedStyle     //同currentStyle作用相同,但是适用于FF、opera、safari、chrome。

研究之后发现了可以用getComputedStyle来获取

getStyle: function(element,attr){
     return (el.currentStyle?el.currentStyle:getComputedStyle(el,null))[attr];
}
324860
  • logo
    • HI, THERE!I AM MOFEI

      (C) 2010-2024 Code & Design by Mofei

      Powered by Dufing (2010-2020) & Express

      IPC证:沪ICP备2022019571号-1