jQuery 单击事件 - 如何判断是否单击了鼠标或按下了 Enter 键?

发布于 2024-12-04 14:46:54 字数 524 浏览 0 评论 0原文

我对我的网站的可用性有疑问。我有一组选项卡,每个选项卡都包含一个表单。当您单击选项卡链接时,它将焦点置于选项卡内容正文中的第一个文本框。以鼠标为中心的人喜欢这个“功能”。问题是当面向键盘的用户使用键盘上的 TAB 键来浏览选项卡时。他们在想要查看的选项卡上按 Enter 键,单击事件触发并显示选项卡,但焦点会放在文本框上,从而完全调整其选项卡顺序。因此,当他们再次点击选项卡时,他们想要转到屏幕上的下一个选项卡,但由于焦点已移动到表单内,他们无法轻松使用键盘到达下一个选项卡。

因此,在单击事件中,我需要确定他们是否确实使用鼠标按钮单击了它。这可能吗?我的第一次尝试是这样的:

$("#tabs li a").click(function(e) {
  var tab = $(this.href);
  if(e.keyCode != 13)
    $("input:first", tab).focus();
});

但是 keyCode 始终为 0。which 属性也始终为 0。请帮忙!

I have a usability concern on a web site of mine. I have a set of tabs, each containing a form. When you click on the tab link, it gives focus to the first textbox in the tab content body. Mouse-oriented people love this "feature". The problem is when keyboard-oriented users use the TAB key on their keyboard to go through the tabs. They hit enter on the tab they want to look at, the click event fires and the tab shows up, but focus is given to the textbox, adjusting their tab order completely. So when they hit tab again, they want to go to the next tab on the screen, but since focus was moved inside the form, they can't easily get to the next tab using the keyboard.

So, inside the click event I need to determine if they actually clicked on it with a mouse button. Is this possible? My first attempt was this:

$("#tabs li a").click(function(e) {
  var tab = $(this.href);
  if(e.keyCode != 13)
    $("input:first", tab).focus();
});

But keyCode is always 0. The which property is also always 0. Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小兔几 2024-12-11 14:46:54

这是我想出的解决方案,它非常简单。我在选项卡链接上捕获了 keydown,并在 keyCode 为 13 时触发了 click 事件。幸运的是, trigger< /code>函数允许我们将额外的参数传递给事件处理程序...

$("#tabs li a").keydown(function(e) {
  if(e.keyCode == 13) {
    $(this).trigger("click", true);
    e.preventDefault();
  }
});

所以我只需更改我的点击处理程序即可接收新参数并使用它...

$("#tabs li a").click(function(e, enterKeyPressed) {
  if(enterKeyPressed)
    alert("Enter key");
  else
    alert("Clicked");
});

我放置了一个 jsFiddle 上的演示。感谢所有阅读该问题的人。

Here's the solution I came up with, it's surprisingly simple. I trapped keydown on the tab links, and triggered the click event when keyCode was 13. Luckily, the trigger function allows us to pass extra parameters to the event handler...

$("#tabs li a").keydown(function(e) {
  if(e.keyCode == 13) {
    $(this).trigger("click", true);
    e.preventDefault();
  }
});

So I just had to change my click handler to receive the new parameter and use it...

$("#tabs li a").click(function(e, enterKeyPressed) {
  if(enterKeyPressed)
    alert("Enter key");
  else
    alert("Clicked");
});

I put up a demo on jsFiddle as well. Thanks to everyone who read the question.

风向决定发型 2024-12-11 14:46:54

对我有用的一个更简单的解决方案是仅检查事件中是否传递了任何鼠标坐标。

document.addEventListener('click', function(e) {
        //if the event object is passed with mouse coordinates, 
        if(e.screenX && e.screenY){
            //The mouse was clicked
        }else{//The enter key was pressed}

});

An even simpler solution that worked for me was to just check whether there were any mouse coordinates passed with the event.

document.addEventListener('click', function(e) {
        //if the event object is passed with mouse coordinates, 
        if(e.screenX && e.screenY){
            //The mouse was clicked
        }else{//The enter key was pressed}

});

七颜 2024-12-11 14:46:54

全局“焦点”变量是否有效,它会在给定选项卡上使用选项卡后禁用鼠标设置的焦点,直到鼠标移动到新块。

这不是您要求的功能,但我相信它可能会给您带来您想要的功能。

例如。鼠标悬停在选项 5 上,按下 Tab 键,现在将 5 存储在变量中,在聚焦于其他内容之前不允许将焦点设置为 5,但一旦聚焦于其他内容,全局就会变回 -1。

我承认这不是最干净的解决方法。

Would a global "focus" variable work, which disable focus on mouse setting after tab usage on a given tab until a mouse is moved to a new block.

This would not be the feature your requesting, but I believe it might give you what your looking for.

eg. mouse hoovers option 5, you hit tab, now you store the 5 in the variable, disallowing focus to 5 until something else been focused on, but as soon something else is focused, global is turned back to -1.

Not cleanest workaround I admit that freely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文