我正在寻找一个 DOM 选择器(读取 css 或 javascript 选择器),它允许我获取由元素的 ::after
插入的文本修饰符。
最小代码示例:
p.something::after {
content: "content is now readable";
background-color: yellow;
color: red;
font-weight: bold;
}
<p class='something'></p>
<p>
Something else
</p>
因此,重新表述一下问题,如何使用 javascript/css 选择器获取第一段 ( content is now readable
) 的内容。
我想排除任何库,因为它们可能不可用,并且正在寻找普通的 JS 或 CSS 答案。
最佳答案
尝试使用 getCompulatedStyle
及其第二个参数:
function readAfterPseudo(el) {
var cs = getComputedStyle(el, '::after');
console.log(cs.content);
}
readAfterPseudo(document.querySelector('.something'))
更多信息可以在 MDN 上找到:getComputedStyle
关于javascript - 用于通过::after 修饰符添加的内容的 DOM 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61060743/