gpt4 book ai didi

jquery - 如何访问:before element?

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:37 26 4
gpt4 key购买 nike

我有以下 SASS 代码

a.unfavorite{
width: 20px;
background-position: -71px -28px;
text-decoration: none !important;
&:before {
content: "un-favorite";
background: #000;
color: #fff;
}
}

在 JQuery 中,我希望能够访问 :before 元素以修改它。

我尝试做类似这样的事情 $('.unfavorite:before') 但没有产生任何结果。我做错了吗?

最佳答案

CSS 伪元素在技术上不是 DOM 的一部分,因此不能使用 JavaScript 访问。

您可以改为为第二个类添加样式(第一个类之后的任何位置)并使用 JavaScript/jQuery 添加/删除该类。

CSS:

a.unfavorite2:before {
content: "un-favorite2";
}

jQuery:

$(this).toggleClass('unfavorite2');

http://jsfiddle.net/mblase75/nvqKs/

关于jquery - 如何访问:before element?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14241950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com