为什么点击事件在返回时被触发? [英] Why is click event triggered on return?

查看:61
本文介绍了为什么点击事件在返回时被触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery API 中指出

In the jQuery API it states

只有在以下一系列确切的事件之后才触发click事件:

The click event is only triggered after this exact series of events:

  1. 当指针位于鼠标的内部时,按下鼠标按钮 元素.
  2. 释放鼠标按钮,而鼠标指针处于 在元素内部.
  1. The mouse button is depressed while the pointer is inside the element.
  2. The mouse button is released while the pointer is inside the element.

但是,我刚刚注意到,在Firefox 39中,当我选择一个输入按钮元素,然后按回车键或空格键时,也会触发该事件.这是为什么?还有其他触发点击事件的事件吗?

However, I just noticed that in Firefox 39 the event is also triggerd when I select an input button element and then press return or the spacekey. Why is that? are there also other events that trigger the click event?

这里是一个示例,请参见 jFiddle .如果我用鼠标按下按钮,它将按预期更改颜色.但是,如果我选择按钮并按回车键或空格键,它也会更改颜色.

Here is an example, see the jFiddle. If I press the button with the mouse it changes the color as expected. But it also changes the color if I select the button and press return or spacekey.

<style>
div{
  width: 200px;
  height: 200px;
  background-color: green;
}

.red{
  background-color: red;
}
</style>

<input type='button' class='button' value='change color'>
<div></div>

<script>
$(document).ready(function(){
      $('.button').click(function(e){    
        $('div').toggleClass('red');
      });
});
</script>

推荐答案

jQuery API文档具有误导性.它描述的过程是用鼠标或其他指针设备触发click事件的唯一方法. W3C建议浏览器在元素处于焦点且确定的位置时触发onclick事件键盘输入发生.提出此建议的原因是增加了可访问性.

The jQuery API documentation is misleading. The process it describes is the only way to trigger the click event with the mouse or other pointer device. The W3C recommends that browsers trigger the onclick event when the element is in focus and certain keyboard inputs happen. The reason for this recommendation is increased accessibility.

这篇关于为什么点击事件在返回时被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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