如果类不是.active,则jQuery动画

JBo*_*oom 0 jquery jquery-animate

我正在尝试动画我可以使用的导航的背景颜色

$(".mainNav li a").hover(function () {
    $(this).stop().animate({ backgroundColor: "#EF4D23" }, 500);
}, function () {
    $(this).stop().animate({ backgroundColor: "#303030" }, 500);
});
Run Code Online (Sandbox Code Playgroud)

我想做的下一步只是做这个动画,如果标签的类不是.active,所以我想:

$(".mainNav li a").hover(function () {
    if ($(this).not(".active")) {
        (function () {
            $(this).stop().animate({ backgroundColor: "#EF4D23" }, 500);
        }, function () {
            $(this).stop().animate({ backgroundColor: "#303030" }, 500);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

但它没有做任何事情或给我一个错误.

谢谢你的帮助.

Ric*_*ton 6

你想使用.is()而不是.not()(.not()将过滤元素,.is()根据它是否匹配选择器返回一个布尔值)

$(".mainNav li a").hover(function () {
    if (!$(this).is(".active")) {
       (function () {
            $(this).stop().animate({ backgroundColor: "#EF4D23" }, 500);
        }, function () {
            $(this).stop().animate({ backgroundColor: "#303030" }, 500);
        });
     }
});
Run Code Online (Sandbox Code Playgroud)