首页 > 解决方案 > 是否可以仅使用 JS 选择 CSS psuedo::after 内容的内容?

问题描述

我找到了引用 JQuery 的帖子,但我想知道是否可以在元素上获取 psuedo::after 元素的文本内容字符串。

截屏: 在此处输入图像描述

如果我选择了有问题的元素,例如document.querySelectorAll('.checkout-form-error')[0];

如何选择文本字符串“需要邮政编码字段”。使用 JS?

标签: javascript

解决方案


这可以通过window.getComputedStyle助手来完成。

var str = window.getComputedStyle(document.querySelector('.mock'), ':after').getPropertyValue('content');
                  
console.log(str)
.mock::after{
  content: "we need to get this text ye?"
}
<div class="mock">

</div>


推荐阅读