2014-07-10 39 views
0

我想添加一个方法到一个模块(认为Web组件),检测高度是否已经设置为其根元素,如果没有 - 基于其他计算高度因素。找出元素是否有声明值

这个模块有一些绝对定位和其他“花哨”的东西,所以正常流量是不可能的。

我想通过document.styleSheets循环和在各cssRule然后解析cssText或寻找“高度”的style对象内部的非空键的selectorText检查根元素的id或一类。

有没有更好的(或标准)方式来做到这一点?

PS - 我可能刚刚在解决问题的同时解决了问题,所以如果遇到同样的问题 - 在这里你去!

+0

您是否尝试过defaultValue属性? – Pikamander2

+0

defaultValue?什么? – tripleZee

回答

0

这里是一个相当粗糙的实现:

isHeightSet: function() { 
    var id = this.elements.root.id, 
     _isHeightSet = false; // the crude part ;-) 

    // go through stylesheets 
    _.each(document.styleSheets, function(styleSheet) { 

     // go through rules 
     _.each(styleSheet.cssRules, function(cssRule) { 

      // if selectorText contains our id 
      if (cssRule.selectorText && cssRule.selectorText.indexOf(id) !== -1) { 

       // check if the height is set 
       if (cssRule.style.height && 
        (cssRule.style.height !== 'auto' && 
        cssRule.style.height !== 'inherit')) { 

        // keep on hacking! 
        _isHeightSet = true; 
       } 
      } 
     }); 
    }); 

    return _isHeightSet; 
} 

大概可以更稳健,但似乎很好地工作。