How do you read CSS rule values with JavaScript?

后端 未结 16 1184
天涯浪人
天涯浪人 2020-11-21 21:02

I would like to return a string with all of the contents of a CSS rule, like the format you\'d see in an inline style. I\'d like to be able to do this without knowing what i

16条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 21:46

    function getStyle(className) {
        document.styleSheets.item("menu").cssRules.item(className).cssText;
    }
    getStyle('.test')
    

    Note : "menu" is an element ID which you have applied CSS. "className" a css class name which we need to get its text.

提交回复
热议问题