如果Div不存在则jQuery

时间:2021-02-27 08:06:26

I have 3 images and have a function so that on mouse rollover they fade in and out. I don't want to do this if the image being rolled over has the class 'selected'.

我有3个图像并具有一个功能,以便在鼠标滚动时它们淡入和淡出。如果正在翻转的图像具有“已选择”类,我不想这样做。

So far I have this code:

到目前为止,我有这个代码:

$(".thumbs").hover(function(){

if (!($(this).hasClass(".selected")){

$(this).stop().fadeTo("normal", 1.0);
},function(){
$(this).stop().fadeTo("slow", 0.3);

}

});

to me the IF statement looks like it should be:

对我来说IF语句看起来应该是:

if (!($(this).hasClass(".selected"))){

but neither work so who knows. When I implement this code my entire javascript stops working, any ideas why?

但是没有工作,所以谁知道。当我实现此代码时,我的整个javascript停止工作,任何想法为什么?

There is virtually NO information on IF statements for jQuery on Google, it's ridiculous. I would greatly appreciate any help!

几乎没有关于谷歌jQuery的IF语句的信息,这很荒谬。我非常感谢任何帮助!

5 个解决方案

#1


The jQuery docs on selectors tells you everything you neeed to know.

选择器上的jQuery文档告诉你需要知道的一切。

$('.thumbs:not(.selected)').hover(function(){
  // ...
})

#2


The problem is that your element has the class "selected" not ".selected". Remove the dot.

问题是你的元素有“选择”类而不是“.selected”。删除点。

#3


Use the not function:

使用not功能:

if ($(this).not(".selected")) {
    ...
}

#4


Maybe it should be

也许它应该是

if (!($(this).hasClass("selected"))){

no . because it knows you're looking for a classname.

不。因为它知道你正在寻找一个类名。

#5


I think your javascript isn't properly formatted -- braces in the wrong places -- and you shouldn't use the dot in the hasClass function. A little better formatting would help readability, too.

我认为你的javascript格式不正确 - 在错误的地方括号 - 你不应该在hasClass函数中使用点。更好的格式化也有助于提高可读性。

$(document).ready( function() {
    $('.thumbs').hover( function() {
                            if (!$(this).hasClass('selected')) {
                                $(this).stop().fadeTo("normal", 1.0);
                            }
                        },
                        function() {
                            $(this).stop().fadeTo("slow", 0.3);
                        }
                 );
});

#1


The jQuery docs on selectors tells you everything you neeed to know.

选择器上的jQuery文档告诉你需要知道的一切。

$('.thumbs:not(.selected)').hover(function(){
  // ...
})

#2


The problem is that your element has the class "selected" not ".selected". Remove the dot.

问题是你的元素有“选择”类而不是“.selected”。删除点。

#3


Use the not function:

使用not功能:

if ($(this).not(".selected")) {
    ...
}

#4


Maybe it should be

也许它应该是

if (!($(this).hasClass("selected"))){

no . because it knows you're looking for a classname.

不。因为它知道你正在寻找一个类名。

#5


I think your javascript isn't properly formatted -- braces in the wrong places -- and you shouldn't use the dot in the hasClass function. A little better formatting would help readability, too.

我认为你的javascript格式不正确 - 在错误的地方括号 - 你不应该在hasClass函数中使用点。更好的格式化也有助于提高可读性。

$(document).ready( function() {
    $('.thumbs').hover( function() {
                            if (!$(this).hasClass('selected')) {
                                $(this).stop().fadeTo("normal", 1.0);
                            }
                        },
                        function() {
                            $(this).stop().fadeTo("slow", 0.3);
                        }
                 );
});