2011-02-17 38 views
7

我有一個有限的理解Javascriptan我想添加一個類到div當我懸停另一個div 現在我可以添加一個類到同一個div但我希望能夠添加讓我們說,當我懸停#second和#third時,將藍色課程首先稱爲第一個div。添加類到另一個懸停另一個(Javascript)

當前代碼:

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

我的直播網站可以看出:www.designinterieurm2.com/dev

+0

這將可能包括某種DOM遍歷,是什麼相關的HTML是什麼樣子? – 2011-02-17 20:28:56

回答

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

感謝它的工作!我所做的錯誤是使用雙引號而不是單引號在我的函數裏 – 2011-02-17 21:14:07

3

只是$("#div_id");

其中#div_id更換$(this)是div的id你正在嘗試改變。

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

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

},function(){ 

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

});