“鼠标悬停”事件,jQuery的使用鼠标“点击上”,而不是(Use mouse 'on cl

2019-11-01 03:42发布

我有这个剧本在这里 ,工作正常,但我需要它点击鼠标来代替鼠标悬停工作。

我曾尝试,但未能成功。 它看起来很简单,但我不能弄明白。 任何人都可以帮忙吗?

代码如下:

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

Answer 1:

尝试这个

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

您可以替换使用.parent() .closest()如果HTML结构是已知的..

要么

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

工作演示



Answer 2:

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


文章来源: Use mouse 'on click' instead of 'mouse over' event, jquery