Getting values of global stylesheet in jQuery

后端 未结 4 1733
长发绾君心
长发绾君心 2020-12-12 04:58

When I use a style sheet definition like this on HTML page scope

#sideBar {
  float: left;
  width: 27.5%;
  min-width: 275;
  ... 
}

the f

4条回答
  •  粉色の甜心
    2020-12-12 05:12

    Here is what I have done. Since all approaches did no really work reliable (cross browser etc.), I came across CSS parser/abstracter? How to convert stylesheet into object .

    First I was about to use some fully blown CSS parsers such as

    1. JSCSSP
    2. jQuery CSS parser

    which are powerful, but also heavyweight. Eventually I ended up with my own little function

    // Get the original CSS values instead of values of the element.
    // @param {String} ruleSelector
    // @param {String} cssprop
    // @returns {String} property of the style
    exports.getCssStyle = function (ruleSelector, cssprop) {
        for (var c = 0, lenC = document.styleSheets.length; c < lenC; c++) {
            var rules = document.styleSheets[c].cssRules;
            for (var r = 0, lenR = rules.length; r < lenR; r++) {
                var rule = rules[r];
                if (rule.selectorText == ruleSelector && rule.style) {
                    return rule.style[cssprop]; // rule.cssText;
                }
            }
        }
        return null;
    };
    

提交回复
热议问题