HTML
<button id="clickMe" tabindex=0>Click Me!</button>
JS
$('#clickMe').button();
$('#clickMe').click(function() {
alert('hey');
});
$(document).keypress(function (e) {
var key = e.keyCode ? e.keyCode : e.which;
//detect when the user has hit enter
if (key == 13) {
//click the focused element
$(document.activeElement).click();
}
});
Why does this alert fire twice when you hit tab to focus the button and enter for the keypress event, but only fires once when you click the button with the mouse?
示范
EDIT: tab + enter doesn't work at all in IE 10
因为当焦点位于按钮上时按“Enter”键会本机触发“click”事件。你also获取“按键”事件,然后再次触发“单击”事件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)