在jQuery中,我需要做一个if语句,看看$this是否不包含类'.selected'。
$(".thumbs").hover(function(){
$(this).stop().fadeTo("normal", 1.0);
},function(){
$(this).stop().fadeTo("slow", 0.3);
});
基本上,当这个函数运行(悬停),我不想执行淡入如果类。Selected '被附加到div中,这将意味着图像将处于完全不透明度,以表示它被选中。在谷歌上搜索没有运气,即使这是一个如何使用IF语句的简单问题…
$(".thumbs").hover(
function(){
if (!$(this).hasClass("selected")) {
$(this).stop().fadeTo("normal", 1.0);
}
},
function(){
if (!$(this).hasClass("selected")) {
$(this).stop().fadeTo("slow", 0.3);
}
}
);
在悬停的每个部分中放入if将允许您动态更改选择类,悬停仍然可以工作。
$(".thumbs").click(function() {
$(".thumbs").each(function () {
if ($(this).hasClass("selected")) {
$(this).removeClass("selected");
$(this).hover();
}
});
$(this).addClass("selected");
});
作为一个例子,我还附加了一个单击处理程序来将所选类切换到所单击的项目。然后在前一项上触发悬停事件,使其淡出。