document.defaultView.getComputedStyle与getPropertyValue

1.document.defaultView.getComputedStyle 这是w3c标准方法,取得元素的样式信息,因为有些样式是在外部css文件定义的,所以用element.style是取不到的 如果是IE,可以用 element.currentStyle["name"] 。

下面是一个兼容浏览器的方法:

  function GetCurrentStyle (obj, prop)
   {
      if (obj.currentStyle) //IE
      {
          return obj.currentStyle[prop];
      }
      else if (window.getComputedStyle) //非IE
      {
          propprop = prop.replace (/([A-Z])/g, "-$1");
          propprop = prop.toLowerCase ();
          return document.defaultView.getComputedStyle(obj,null)[propprop];
      }
     return null;

   } 

2.getPropertyValue方法可以用来获取元素中指定的css属性值,支持W3C标准.但不支持IE浏览器。getPropertyValue必须配合getComputedStyle方法一起使用。

兼容IE和FF等浏览器获取元素css属性值的方法:

css_value=window.getComputedStyle.getPropertyValue(css_name)

 

posted @ 2013-05-09 21:52  Joy Ho  阅读(261)  评论(0编辑  收藏  举报