使用jQuery在选择器之前操作CSS [英] Manipulate CSS before selector with jQuery

查看:101
本文介绍了使用jQuery在选择器之前操作CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问CSS :之前:之后选择器与jQuery。我试过几个方法,但似乎CSS选择器不能被DOM看到。我想操作的是给元素一个动态 background-color

I'm trying to access the CSS :before and :after selectors with jQuery. I have tried a few methods, but it seems that the CSS selectors can't be seen by the DOM. What I'm trying to manipulate is to give the element a dynamic background-color.

例如: / p>

For example:

.className:before {background: red;}

在某些时候,我想将此颜色更改为动态:

At some point, I want to change this color to be dynamic:

$('.clasName:before').css('background-color', bgColor);   //this method doesn't work

也尝试过:

$('.clasName').addClass('change').attr('data-background', bgColor); 
//this method works only for content property

CSS

.className.change:before {
 background: attr(data-background);
}

它不工作。

推荐答案

您不能使用javascript更改伪元素的样式,但是您可以在 .className 中添加< span> ,然后定位。

You can't change the style of pseudo elements with javascript, but you could add a <span> inside of your .className and then target that.

$('.className span').css('background-color', bgColor);

这篇关于使用jQuery在选择器之前操作CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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