使用鼠标“单击"而不是“鼠标悬停"事件,jQuery [英] Use mouse 'on click' instead of 'mouse over' event, jquery
本文介绍了使用鼠标“单击"而不是“鼠标悬停"事件,jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在此处拥有此脚本,该脚本可以正常工作,但我需要使其通过鼠标单击工作鼠标悬停.
I have this script here which works fine, but I need to make it work on mouse click instead of mouse hover.
我已经尝试过,但是失败了.看起来很简单,但我无法弄清楚.有人可以帮忙吗?
I have tried, but failed. It looks simple, but I can't figure it out. Can anyone help?
代码是:
$(".cat").hover(function(){
$(this).parent().parent().find(".sub_menu").hide();
},
function() {
$(this).parent().parent().find(".sub_menu").show();
});`
下面的
代码可以在Firefox中使用,而不是在Firefox中使用.
code below works in ie, not in 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().
You can replace the .parent() by using .closest()
if the HTML structure is known..
或
$(".cat").on('click', function(e){
e.preventDefault();
$(this).closest('.menu').find(".sub_menu").toggle();
});
WORKING DEMO
这篇关于使用鼠标“单击"而不是“鼠标悬停"事件,jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文