Jquery 简单鼠标悬停赢得´;行不通
我有一个奇怪的问题: 我设置了一个悬停的h2,jQuery应该检查h2是否有一个类“main active”: 不知何故,当它的类为“main active”时,它继续将h2悬停在白色上 有什么想法吗?试试这个解决方案:Jquery 简单鼠标悬停赢得´;行不通,jquery,hover,Jquery,Hover,我有一个奇怪的问题: 我设置了一个悬停的h2,jQuery应该检查h2是否有一个类“main active”: 不知何故,当它的类为“main active”时,它继续将h2悬停在白色上 有什么想法吗?试试这个解决方案: $('h2').hover(function (){ if (!$(this).hasClass('main-active')){$(this).css('color','white');} }); $('h2').mouseout(function (){$(th
$('h2').hover(function (){
if (!$(this).hasClass('main-active')){$(this).css('color','white');}
});
$('h2').mouseout(function (){$(this).css('color','black');});
文档:当悬停函数本身绑定mouseover/mouseout事件时,为什么要手动使用悬停然后再使用mouseout 有什么问题吗
$('h2').hover(function() {
var e = $(this);
if (e.hasClass('main-active')){e.css('color','white');}
}, function() {
var e = $(this);
if (e.hasClass('main-active')){e.css('color','black');}
});
});
注意:最好只在上面使用$()一次,并将其缓存到一个变量中,这样jQuery就不需要每次都将元素解析到一个对象中,就像上面的代码:var e=$(this)在
main active
中缺少句点
$('h2').hover(function() {
var e = $(this);
if (e.hasClass('main-active')){e.css('color','white');}
}, function() {
var e = $(this);
if (e.hasClass('main-active')){e.css('color','black');}
});
});