简体   繁体   English

如何在属性中获取CSS类名称值部分

[英]How to get CSS class name value part within the property

Is there any option to get the class name value within property. 是否有任何选项可以在属性中获取类名称值。 Here, I need 12. 在这里,我需要12。

 .grid-12{ grid-template-columns: repeat(12, 1fr); } 

Thanks in advance. 提前致谢。

Using something like that 使用类似的东西

document.querySelector('div').style['grid-template-columns'] you can obviously get the value and then parse it but in my eyes that looks kinda ugly. document.querySelector('div').style['grid-template-columns']您显然可以获取该值,然后对其进行解析,但是在我看来这看起来很丑。

The best you can to do is to use a css parser to transform the css into a AST tree and iterate over it. 最好的办法是使用CSS解析器将CSS转换为AST树并对其进行迭代。 That of course has its own trade-offs and usually doesn't worth to do it in the frontend, its not even 100% sure that the parser will give you that value. 当然,这是有其自身取舍的,通常不值得在前端进行,甚至不能100%保证解析器会为您提供该值。 eg I see that your snippet in a parser is transform to the following AST tree 例如,我看到您在解析器中的代码段已转换为以下AST树

{
  "parentStyleSheet": null,
  "cssRules": [
    {
      "parentRule": null,
      "parentStyleSheet": "[Circular ~]",
      "selectorText": ".grid-12",
      "style": {
        "0": "grid-template-columns",
        "length": 1,
        "parentRule": "[Circular ~.cssRules.0]",
        "_importants": {
          "grid-template-columns": ""
        },
        "__starts": 8,
        "grid-template-columns": "repeat(12, 1fr)"
      },
      "__starts": 0,
      "__ends": 55
    }
  ]
}

So again, you have to manually extract the 12 再次,您必须手动提取12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM