简而言之,如何在悬停this fiddle
时将标签突出显示为红色背景答案 0 :(得分:2)
为什么不为此使用一些纯粹的CSS:
#nav ul li:hover { background-color: red; }
修改强>
如果您尝试使用jQuery(作为学习经验),我会定义一个名为hoverRed的新css类
.hoverRed { background-color: red; }
然后使用悬停功能:
$("#nav ul li").hover(function() {
$(this).addClass("hoverRed");
}, function() {
$(this).removeClass("hoverRed");
});
第一个函数在悬停开始时被调用,第二个函数在悬停结束时被调用(并且鼠标离开)
答案 1 :(得分:1)
使用此:
#nav ul li:hover
{
background: red;
}
<强>更新强>: 这是您的mouseenter和mouseleave事件的fiddle。这是我添加的代码。
CSS
.lihover{background: red;}
的jQuery
$("li").mouseenter(function(){
$(this).addClass("lihover");
}).mouseleave(function(){
$(this).removeClass("lihover");
});