我有一个链接,我想在鼠标悬停时添加类,如下所示:
$("#navbarlarge").on("mouseover","a",function(){
$(this).addClass("linkeffect");
})
问题是我还想为链接中的::before 和::after 伪元素添加一个类,试过了但不起作用:
$(this).find("::before").addClass("beforeeffect");
如何将这个选择器与伪元素结合起来?
最佳答案
你不能。伪元素不能被 JavaScript 选择,因此也不能被 jQuery 选择。
将类附加到元素本身,并使用 #navbarlarge a
应用 CSS...等一分钟。
完全放弃 jQuery。
#navbarlarge a:hover {
/* styles to apply to hovered link */
}
#navbarlarge a:hover::before {
/* styles to apply to hovered link before pseudo */
}
#navbarlarge a:hover::after {
/* styles to apply to hovered link after pseudo */
}
关于javascript - jQuery $(this) 伪选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27817632/