3

I'm trying to write some helper functions so I can edit a css class through javascript. Here is what I have, but I don't understand why it keeps returning "undefined". Im using chrome for testing.

    function getRule(name, doDelete) { //takes a rule name and boolean

    if(document.styleSheets) { //check if there are stylesheets

        for(var i = 0; i < document.styleSheets.length; i++) { //loop through style sheets

            var currentSheet = document.styleSheets[i];
            if(currentSheet.cssRules) { //if the sheet has rules

                for(var h = 0; h < currentSheet.cssRules.length; h++) { //loop through css rules

                    var currentRule = currentSheet.cssRules[h];
                    if(currentRule.selectorText == name) { //if rule name is equal to the name given

                        return currentRule; //return the css rule

                    }

                }

            }

        }

    }

}

css being used:

    .menuopen {

    margin-left: auto;
    margin-right: auto;
    text-align: center;
    font-size: 20px;
    width: 960px;
    height: 200px;
    background-color: 3b7ea8;
    box-shadow: inset 0px 2px 5px 3px rgba(0,0,0,0.75);
    -webkit-animation-name: dropAnimation;
    -webkit-animation-duration: 2s;

    }
4

1 回答 1

1

这些问题之一可能是问题吗?
document.styleSheets[n].cssRules 在本地访问页面时
为 null 来自其他域的 CSSStyleSheets 的 cssRules 为 null
回归:当从本地磁盘加载样式表时 cssRules
为 null 这些问题的出现取决于样式表的包含方式以及页面加载方式(从磁盘或从服务器)。

于 2013-06-29T05:26:08.187 回答