使用鼠标'点击'而不是'鼠标悬停'事件,jquery

时间:2012-10-25 23:23:48

标签: jquery mousehover onmouseclick

我有这个脚本here可以正常工作,但我需要让鼠标点击而不是鼠标悬停。

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

代码是:

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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