2012-10-25 249 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(); 
});