JQuery If Div Doesn't Exist

I have 3 images, and I have a function, so when you move the mouse, they disappear and disappear. I do not want to do this if the image that is being rolled has the class "selected".

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);

}

});

for me, the IF statement looks like this:

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

but not one of them works, who knows. When I implement this code, all my javascript stops working, any ideas why?

Information about IF statements for jQuery in Google is practically absent, this is ridiculous. I would really appreciate any help!

+3
source share
5 answers

JQuery selector docs tell you everything you don’t know.

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

, "selected", ".selected". .

+5

not:

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

,

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

no. because he knows that you are looking for a class name.

+1
source

I think your javascript is incorrectly formatted - the brackets are in the wrong places - and you should not use a dot in the hasClass function. A bit better formatting will also help readability.

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

Source: https://habr.com/ru/post/1702814/


All Articles