jQuery If DIV Doesn't Have Class “x”

后端 未结 7 2350
庸人自扰
庸人自扰 2020-11-27 10:54

In jQuery I need to do an if statement to see if $this doesn\'t contain the class \'.selected\'.

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

$(this).stop().fadeTo(\"no         


        
7条回答
  •  盖世英雄少女心
    2020-11-27 10:59

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

    Putting an if inside of each part of the hover will allow you to change the select class dynamically and the hover will still work.

    $(".thumbs").click(function() {
        $(".thumbs").each(function () {
            if ($(this).hasClass("selected")) {
                $(this).removeClass("selected");
                $(this).hover();
            }
        });                 
        $(this).addClass("selected");                   
    });
    

    As an example I've also attached a click handler to switch the selected class to the clicked item. Then I fire the hover event on the previous item to make it fade out.

提交回复
热议问题