[欣赏代码片段] (JavaScript) 你使用过getComputedStyle没有

(function() {

// IE8 ployfill for GetComputed Style (for Responsive Script below)
if (!window.getComputedStyle) {
  window.getComputedStyle = function(el, pseudo) {
    this.el = el;
    this.getPropertyValue = function(prop) {
      var re = /(\-([a-z]){1})/g;
      if (prop == 'float') prop = 'styleFloat';
      if (re.test(prop)) {
        prop = prop.replace(re, function () {
          return arguments[2].toUpperCase();
        });
      }
      return el.currentStyle[prop] ? el.currentStyle[prop] : null;
    }
    return this;
  }
}
/*
window.onload = function() {
  var compStyle = widow.getComputedStyle(document.getElementById('test'), '');
  
  alert(compStyle.getPropertyValue("color"));
  alert(compStyle.getPropertyValue("float"));
  alert(compStyle.getPropertyValue("background-color"));
};
*/

})();

在某些浏览器上处理某些CSS问题,或者当你需要在JavaScript获取某个特定元素的样式时,你可以想到它。

posted @ 2013-07-12 12:27  书生小C  阅读(235)  评论(0编辑  收藏  举报