是否要将元素$("p")返回到应用mouseenter()方法之前的确切颜色?或者我需要知道它在mouseenter()之前是什么颜色,然后在mouseleave()中应用该颜色吗?我想让它恢复到应用mouseleave()时的颜色.除了知道mouseenter()之前的颜色之外,还有别的方法吗?
Jquery代码(我不想将它设置为蓝色,我想将它设置为mouseenter()之前的颜色):
$(document).ready(function () {
$("p").mouseenter(function () {
$(this).css("color","yellow")
})
.mouseleave(function () {
$(this).css("color","blue")
})
})发布于 2016-04-14 15:25:18
发布于 2016-04-14 15:26:33
使用类。在mouseEnter上添加类$(this).addClass("the_new_color"),在mouseLeave上只删除它。$(this).removeClass("the_new_color")。这个css类应该有!important来覆盖初始颜色。
发布于 2016-04-14 15:42:31
最好的方法是,如果使用.toggle(),那么只需要放置“新”类,而不是mousenter()和mouseleave(),只需使用悬浮()。
$( "p" ).hover(function() {
$( this ).toggleClass("blue");
});在css文件中:
.blue {
color: blue;
}https://stackoverflow.com/questions/36627482
复制相似问题