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

查看:16
本文介绍了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天全站免登陆