Javascript Firefox - 如果样式表中存在 @import,则无法查询 cssRules - 错误或预期行为?

标签 javascript css firefox browser

如果 @import 出现在 css 样式表中,我将无法查询 cssRules。 是否符合网络标准? 或者它知道 Firefox 的限制?

注意:我正在从同一域导入 css 文件。

var style_rules = document.styleSheets[0].cssRules;
console.log(style_rules);

底层对象不支持参数或操作 [Break On This Error] var style_rules = document.styleSheets[0].cssRules;

最佳答案

属性 document.styleSheets[0].cssRules 是一个 CSSRuleList (除了在 IE6-8 中,你应该 styleSheets[0].rules 用于 css 规则和 styleSheets[0].imports 用于导入)。这个CSSRuleList有一定数量的CSSRules在列表中。这些规则可以是不同的类型。例如 @import CSSRule 实现了 CSSImportRule接口(interface)和“正常”样式声明 CSSRule 实现了 CSSStyleRule界面。 我们可以通过检查是否 rule.type == IMPORT_RULE 来检测 CSSRule 是否是 @import css 规则,其中 IMPORT_RULE 为 3。如果它是 CSSImportRule,我们可以获取其 styleSheet 属性以获取导入样式表中的 css 规则并重复上述过程。

可以通过获取 cssText 属性来获取任何 CSSRule 的可解析文本表示:rule.cssText。然而,在 Internet Explorer 6-8 中,我们必须使用 rule.style.cssText

换句话说,我们可以使用以下代码获取样式表中的所有 CSSRule(包括其导入)。我还将一个工作示例放入 jsfiddle .请注意,此代码在 IE6-8 中不起作用。对于此解决方案,我建议您在 SO here 上检查我的解决方案是否存在另一个问题。 .

/**
 * Get the css rules of a stylesheet which apply to the htmlNode. Meaning its class
 * its id and its tag.
 * @param CSSStyleSheet styleSheet
 */
function getCssRules(styleSheet) {
    if ( !styleSheet )
        return null;

    var cssRules = new Array();
    if (styleSheet.cssRules) {
        var currentCssRules = styleSheet.cssRules;
        // Import statement are always at the top of the css file.
        for ( var i = 0; i < currentCssRules.length; i++ ) {
            // cssRules contains the import statements.
            // check if the rule is an import rule.
            if ( currentCssRules[i].type == 3 ) {
                // import the rules from the imported css file.
                var importCssRules = getCssRules(currentCssRules[i].styleSheet);
                if ( importCssRules != null ) {
                    // Add the rules from the import css file to the list of css rules.
                    cssRules = addToArray(cssRules, importCssRules);
                }
                // Remove the import css rule from the css rules.
                styleSheet.deleteRule(i);
            }
            else {
                // We found a rule that is not an CSSImportRule
                break;
            }
        }
        // After adding the import rules (lower priority than those in the current stylesheet),
        // add the rules in the current stylesheet.
        cssRules = addToArray(cssRules, currentCssRules);
    }


    return cssRules;
}

/**
 * Since a list of rules is returned, we cannot use concat. 
 * Just use old good push....
 * @param CSSRuleList cssRules
 * @param CSSRuleList cssRules
 */
function addToArray(cssRules, newRules) {
    for ( var i = 0; i < newRules.length; i++ ) {
        cssRules.push(newRules[i]);
    }
    return cssRules;
}

/**
 * Finds all CSS rules.
 */
function getCSSRules() {
    var cssRules = new Array();

    // Loop through the stylesheets in the html document.
    for ( var i = 0; i < document.styleSheets.length; i++ ) {
        var currentCssRules = getCssRules(document.styleSheets[i])
        if ( currentCssRules != null )
            cssRules.push.apply(cssRules, currentCssRules);
    }

    return cssRules;
}

// An array of all CSSRules.
var allCSSRules = getCSSRules();
    for ( var i = 0; i < allCSSRules.length; i++ ) {
        console.log(allCSSRules[i].cssText);
    }

关于Javascript Firefox - 如果样式表中存在 @import,则无法查询 cssRules - 错误或预期行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8274402/

相关文章:

html - 如何将样式应用于 HTML 子文档?

css - 边距影响 Firefox 中的其他对象

javascript - nodejs + WebSockets - 通过消息拒绝连接

javascript - 获取 0 的 documentElement 返回什么

java - GWT 中的多个 javascript 异常处理

ajax - Firefox 似乎正在对并行 ajax 请求的成功函数进行排队,可能是因为 setTimeout?

firefox - 主干导航在 Firefox 中触发两次

javascript - jQuery 的 getScript 和本地文件系统——限制/替代方案?

html - 使用 Ionic 限制文本高度和宽度的省略号

javascript - 使用 Angular 获取 div 的 clientHeight?