2012-10-25 95 views
0

我有這個腳本here它工作正常,但我需要使它在鼠標單擊,而不是鼠標懸停工作。使用鼠標'點擊'而不是'鼠標懸停'事件,jquery

我試過了,但失敗了。它看起來很簡單,但我無法弄清楚。誰能幫忙?

代碼:下面

$(".cat").hover(function(){ 
    $(this).parent().parent().find(".sub_menu").hide(); 
}, 
function() { 
    $(this).parent().parent().find(".sub_menu").show(); 
});` 

代碼在IE中,而不是在Firefox瀏覽器。

$(".cat").on('click', function(){ 
    $(this).parent().parent().find(".sub_menu").toggle(); 
}); 
+0

請問您可以添加不適用於該問題的代碼嗎? (點擊標籤下方的「編輯」按鈕。)這意味着這個問題將來仍然有用。 – lonesomeday

回答

0

試試這個

$(".cat").on('click', function(e){ 
    e.preventDefault(); 
    $(this).parent().parent().find(".sub_menu").toggle(); 
}); 

您可以通過,如果HTML結構是已知的使用.closest()更換.parent()..

OR

$(".cat").on('click', function(e){ 
     e.preventDefault(); 
     $(this).closest('.menu').find(".sub_menu").toggle(); 
}); 

WORKING DEMO

+0

Adeneo的解決方案在Firefox中不起作用(在ie中工作),所以我會和Sushanth的.closest()一起去。謝謝! – pano

0
$(".cat").on('click', function(){ 
    $(this).parent().parent().find(".sub_menu").toggle(); 
});