如何通过Javascript更改伪:before元素的内容值
我有由CSS构造的grap,它由JS动态更改.我用伪元素将图形最大值显示为:
I have the grap constructured by CSS, which is dynamically changes by JS. I show graph max value by pseudo element as:
.graph:before {
content:""; //value that want set by JS
position:absolute;
top:0;
left:0;
}
这就是为什么我需要通过JS设置此值的原因.我尝试了$(".graph:before").css("content", hh);
,但没有帮助.如何获得该价值?
That's why I need to set this value by JS. I tried $(".graph:before").css("content", hh);
but it didn't help. How to get that value?
Update (2018): as has been noted in the comments, you now can do this.
您无法通过JavaScript修改伪元素,因为它们不是DOM的一部分.最好的选择是使用所需的样式在CSS中定义另一个类,然后将其添加到元素中.由于从您的问题看来这似乎不可能,所以也许您需要研究使用真正的DOM元素而不是伪元素.
You can't modify pseudo elements through JavaScript since they are not part of the DOM. Your best bet is to define another class in your CSS with the styles you require and then add that to the element. Since that doesn't seem to be possible from your question, perhaps you need to look at using a real DOM element instead of a pseudo one.