用户名:mfr432
本文介绍来自了使用j**ascript,jQuery实现修片改before,after伪类的样式,分享给大家,具体如下:
最近遇到一个需要改变:before,:酸本社after伪类的样式,发现问答css中并不能直接选择某一准又为记变外个元素的:before和:after伪类元素,所以特总结了使用j**ascript和jQu若附怕行呀套评袁检程ery改变元素的:before和:afte是按上能施岁儿r伪类样式的方法。
类如如下的html及样式
<pclass="red">h**ello,areyoukain?</p>
css样式
.r
诉扬管微块重德叶否ed:before{content:'red',background-color:red}1.使用j**ascript或者jQuery切换,增加元素的类
.green:be停药别fore{content:'green';必号额秋运名background-color:green;}$('p').addClass('green');2.在存在的style文档中动态插入样式
document.styleSheet[0].程攻技迅律件次包addRule('.red:before','backgroun宽d-color:green')转似西;document.sty变应苗般他阻担leSheet[0].insertRule('.red:before{background-color:green}',0);3.创建一份新的样式表,并使用J**aS完状留布垂仅去树称较cript或jQuert将其插入到中
varstyle=d湖岩未界乡曲叶丰余ocument.creatElement('style');document.head.appen航息继dChild(style);sheet=style.sheet;sheet.addRule('.red:before','background-color:green');sheet.insertRule('.red责做眼着田进法械应挥损:before{backg田久坏未觉分引进round-color:green}',0);//jQuery$('<style>.red:before{background-color:green}</style>').appendTo('head');4.使用h**TML5的data-属性,在属性中使用attr()动态修改。
在p标签中增加data-attr="red"属性,然后
$('red').attr('data-attr','green');以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。