Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 找出元素是否有声明的值_Javascript_Css_Dom_Stylesheet_Detection - Fatal编程技术网

Javascript 找出元素是否有声明的值

Javascript 找出元素是否有声明的值,javascript,css,dom,stylesheet,detection,Javascript,Css,Dom,Stylesheet,Detection,我想向一个模块(想想web组件)添加一个方法,用于检测是否已将高度设置为其根元素,如果未设置,则根据其他因素计算其高度 这个模块有一些绝对的定位和其他“奇特”的东西,所以正常的流程是不可能的 我正在考虑遍历document.styleSheets并检查每个cssRule的selectorText中根元素的id或类,然后解析cssText或在style对象中查找一个非空的“height”键 有更好的(或标准的)方法吗 PS-我可能在制定问题时刚刚解决了它,所以如果你遇到同样的问题-来吧 这是一个相

我想向一个模块(想想web组件)添加一个方法,用于检测是否已将高度设置为其根元素,如果未设置,则根据其他因素计算其高度

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

我正在考虑遍历
document.styleSheets
并检查每个
cssRule
selectorText
中根元素的id或类,然后解析
cssText
或在
style
对象中查找一个非空的“height”键

有更好的(或标准的)方法吗


PS-我可能在制定问题时刚刚解决了它,所以如果你遇到同样的问题-来吧

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

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;
}

可能更健壮,但似乎工作正常。

您是否尝试过defaultValue属性?defaultValue?什么的?