首页 > 解决方案 > How to get pseudo element's computed value?

问题描述

I have a counter in a psuedo class in my CSS, something like this,

h2::before {
    counter-increment: section;
    content: "Section " counter(section) ": ";
}

and I want the computed value of counter.

I have tried using

getComputedStyles(document.getElementById("1"), '::before')

but that returns Section " counter(section) ":, whereas I am trying to get the actual value that it displays. which is Section 1:

This is the demo HTML snippet that I have been testing with,

body {
  counter-reset: section;
}

h2::before {
  counter-increment: section;
  content: "Section " counter(section) ": ";
}
<h2 id="1">HTML Tutorial</h2>

标签: javascriptchromiumpuppeteer

解决方案


不幸的是,使用当前的 CSS 规范是不可能的。在这个答案中查看为什么以及该怎么做


推荐阅读