在悬停另一个时将类添加到div(Javascript)

时间:2022-09-10 12:09:22

I have a limited understanding of Javascriptan I am trying to add a class to a div when I hover another div Right now I can add a class to the same div but I would like to be able to add let's say the class blue to a div called first when I hover #second and #third.

我对Javascriptan有一个有限的理解我正在尝试将一个类添加到div当我悬停另一个div现在我可以添加一个类到同一个div但我希望能够添加让我们说类蓝色到div当我悬停#second和#third时首先调用。

current code :

当前代码:

    
    $(document).ready(function() {     
    $("#second, #third").hover(function(){     
    $(this).addClass("hover");    
    $(this).removeClass("hoverable");      
    },     
    function(){    
    $(this).removeClass("hover");     
        $(this).addClass("hoverable");     
    }  
    );     
    });        

My live website can be seen at : www.designinterieurm2.com/dev

我的实时网站可以在www.designinterieurm2.com/dev上看到

3 个解决方案

#1


18  

$(document).ready(function() {     
    $('#second, #third').hover(function(){     
        $('#first').addClass('blue');    
    },     
    function(){    
        $('#first').removeClass('blue');     
    });
});   

#2


3  

Just replace $(this) with $("#div_id");

只需用$(“#div_id”)替换$(this);

where #div_id is the id of the div you're trying to change.

其中#div_id是您尝试更改的div的ID。

#3


0  

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

    $(this).addClass("current");
    $(this).find("ul").show();

},function(){

    $(this).find("ul").hide();
    $(this).removeClass("current");

});

#1


18  

$(document).ready(function() {     
    $('#second, #third').hover(function(){     
        $('#first').addClass('blue');    
    },     
    function(){    
        $('#first').removeClass('blue');     
    });
});   

#2


3  

Just replace $(this) with $("#div_id");

只需用$(“#div_id”)替换$(this);

where #div_id is the id of the div you're trying to change.

其中#div_id是您尝试更改的div的ID。

#3


0  

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

    $(this).addClass("current");
    $(this).find("ul").show();

},function(){

    $(this).find("ul").hide();
    $(this).removeClass("current");

});