javascript - 为什么单击按钮会触发焦点?

标签 javascript jquery html

为什么单击按钮也会触发 focus 事件?

如果我有这个

$('button').on('click', function(e){
   console.log('This was a ' + e.type); 
});
$('button').on('focus', function(e){
   console.log('This was a ' + e.type); 
});

和 HTML

<button>Click me</button>

当我单击按钮时,两个事件都会触发...为什么?

http://jsfiddle.net/PACXn/

最佳答案

因为 <button>始终触发焦点事件(当您单击它时)。正如您可以在 http://api.jquery.com/focus/ 中阅读的那样

"The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>, <select>, etc.) and links (<a href>)."

关于javascript - 为什么单击按钮会触发焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23707478/

相关文章:

javascript - 使用 jspdf 和 html2canvas 的动态 html 的多个 pdf 页面

javascript - jquery fadeOut() 多次使用同一元素

javascript - 在另一个函数中调用函数

javascript - 嵌套Tree结构对象尝试提取并获取Json对象信息

jquery - 如何删除下拉菜单 ul li 和 jquery 上的文本突出显示

html - 图像的位置大于窗口大小

javascript - SVG 圆在 <a> 标签中时不出现

javascript - 从包含 length 属性的对象创建数组

javascript - 屏幕阅读器的闪烁文本

jquery - .html() 和 .append() 哪个更快?