怎么使用JS修改CSS的伪类

html-css018

怎么使用JS修改CSS的伪类,第1张

元素.style.属性="值"

例如:

<script>

var obj=document.getElementById("item2")

obj.style.color="#ff0000"

</script>

用js控制css伪类after:只能通过添加样式,然后通过这个样式的伪类来控制吧。。没法直接改。

比如:

html:

<p>瓦赫塔 阿热 有 doing</p>

css:

p:after{

content:''

position:absolute

background-color:green

width:20px

height:6px

}

js:

var css=function(t,s){

s=document.createElement('style')

s.innerText=t

document.body.appendChlild(s)

}

document.onclick=function(){

css('p:after{background-color:red}')

}

貌似么有啊……javascript修改css其实就是写到元素的style属性上,:before,:after伪类是虚拟元素……怎么改啊……要是非得要修改的话,建议切换.info的class试试,设计两个有:before的class,需要修改是切换有class的那个元素 ,比如

.info:before{

content:"infomation"

border:1px solid #ccc

}

.info_other:before{

content:"infomation"

border:2px solid #000

},

然后需要修改的时候把.info改成.info_other