从javascript更改:before selector [英] Change the :before selector from javascript

查看:139
本文介绍了从javascript更改:before selector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在javascript之前更改#abc样式的内容?

How to change the :before content of #abc style from javascript ?

<script>
document.getElementById('abc').style.content='-';
</script>

<style>
#abc:before{content:"+";}
</style>


推荐答案

不,您不能访问 :之前:之后,因为它们不是DOM的一部分。但是,您仍然可以通过使用CSS类实现您的目标:

No, you cannot access :before or :after from javascript, because they are not a part of the DOM. However you can still achieve your goal by using CSS classes:

<script>
    document.getElementById('abc').className = "minus";
</script>

<style>
    #abc:before {content: "+";}
    #abc.minus:before {content: "-"}
</style>

事实上,这种方法更不引人注意,因为你不能将代码与javascript混合。明天你可能想要改变文本+/-来说出漂亮的背景图片,在这种情况下你根本不需要触摸javascript代码。

In fact this approach is more unobtrusive, because you don't mix representation with javascript. Tomorrow you might want to change text "+/-" to say nice background images, in this case you don't have to touch javascript code at all.

这篇关于从javascript更改:before selector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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