css改变伪元素color,使用jQuery修改:after伪元素的CSS

I use the :after pseudo-element to display a decoration (triangle) after a block (

in my case). The idea is to distinguish the currently selected li from others.

The html follows:

  • One
  • Two
  • Three

and the css:

ul li {

width: 300px;

height: 30px;

border: 1px dashed;

position: relative;

}

li.active::after {

content: " 0020";

display: block;

font-size: 0px;

position: absolute;

left:100%;

top: 0%;

width: 0px;

height: 0px;

background: transparent;

border: 17px solid transparent;

border-left-color: #FF3900;

}

I want to change the border-left-color style attribute of li.active::after pseudo element to match the background-color of the

element with class=active.

I came up with the following jquery:

$("ul li").click(function() {

$("ul li").removeClass("active");

$(this).addClass("active");

$("li.active::after").css('border-left-color', $(this).css('background-color'));

});

This doesn't work as expected. Any help is appreciated.

解决方案

you can't select pseudo elements such as :before and :after with jquery. But in your case you can do a workaround to use the parent li's style on the :after and thus match the border-color property

CSS updated:

ul li {

width: 300px;

height: 30px;

border: 1px dashed;

position: relative;

}

li:first-of-type.active {

border-left-color: green; // your exact colors here

}

li:nth-of-type(2).active {

border-left-color: orange;

}

li:last-of-type.active {

border-left-color: purple;

}

li.active::after {

content: " 0020";

display: block;

font-size: 0px;

position: absolute;

left:100%;

top: 0%;

width: 0px;

height: 0px;

background: transparent;

border: 17px solid transparent;

border-left-color: inherit;

}

and then just remove the line of js that includes the :after selector. not needed anymore

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值