[欣賞代碼片斷] (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獲取某個特定元素的樣式時,你能夠想到它。瀏覽器

相關文章
相關標籤/搜索