How can you determine if a css class exists with Javascript?

前端 未结 10 1033
闹比i
闹比i 2020-11-27 04:14

Is there a way to determine whether or not a css class exists using JavaScript?

10条回答
  •  半阙折子戏
    2020-11-27 04:38

    Oneliner:

    [].slice.call(document.styleSheets)
    .reduce( (prev, styleSheet) => [].slice.call(styleSheet.cssRules))
    .reduce( (prev, cssRule) => prev + cssRule.cssText)
    .includes(".someClass")
    

提交回复
热议问题