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();
}
});
为什么当您点击tab聚焦按钮和enter按键事件时此警报会触发两次,但当您用鼠标单击按钮时只会触发一次?
编辑: tab+enter在 IE 10 中根本不起作用