使用鼠标’点击’而不是’鼠标hover’事件,jquery

我在这里有这个脚本工作正常,但我需要让鼠标点击而不是鼠标hover。

我试过了,但都失败了。 它看起来很简单,但我无法弄清楚。 有人可以帮忙吗?

代码是:

$(".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(); }); 

试试这个

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

如果已知HTML结构,则可以使用.closest()替换.parent()。

要么

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

工作演示

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