如何通过Javascript更改伪:before元素的内容值 [英] How change content value of pseudo :before element by Javascript

查看:83
本文介绍了如何通过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?

推荐答案

更新(2018):如评论中所述,您现在

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.

这篇关于如何通过Javascript更改伪:before元素的内容值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆