jQuery同时兼容HOVER和FOCUS(鼠标和键盘) [英] jQuery accommodating both HOVER and FOCUS together (mouse and keyboard)

查看:204
本文介绍了jQuery同时兼容HOVER和FOCUS(鼠标和键盘)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个超级菜单,我希望能够通过鼠标悬停(使用鼠标)和焦点(例如通过键盘挂钩)来触发菜单。

I'm building a mega menu where I want to be able to trigger the menu via both a hover (using the mouse) and focus (such as tabbing to it via the keyboard).

这就是我目前所做的:

This is what I'm presently doing:

$(".megaMenu-trigger").focus(function (){$(this).hover()});
$(".megaMenu-trigger").hover(function(){
    // do the stuff
});

这很有效,但我想知道这是处理键盘和鼠标交互的理想方式。

This works, but am wondering if that's the ideal way to handle both keyboard and mouse interactions together.

推荐答案

您可以使用bind方法将多个事件绑定到一个操作,即

you can use the bind method to bind multiple events to one action i.e.

$('.megaMenu-trigger').bind("mouseenter focus mouseleave", 
        function(event) { console.log(event.type); }); 

这篇关于jQuery同时兼容HOVER和FOCUS(鼠标和键盘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆