Why alert isn't showing when pressing spacebar?

时间:2015-06-25 19:08:15

标签: javascript google-chrome

Why we do not see the alert (the result of the alert function in Chrome) when you press the space bar, as opposed to pressing other keys, which gave us the alert. <input type=text onKeyPress="my_keypress($event);"> function my_keypress($event) { var x = ($event.keyCode || $event.which); var x_char = String.fromCharCode(x); alert( 'x: '+x + '\n' + 'x_char: ' + x_char + '\n' + 'altKey: ' + $event.altKey + ' ' + 'ctrlKey: ' + $event.ctrlKey + ' ' + 'shiftKey: ' + $event.shiftKey ); }

1 个答案:

答案 0 :(得分:2)

您的onKeyPress代码应为my_keypress(event);(请注意已移除$代码。)

当您按空格并触发警报时,警报会出现,但在释放空间后立即关闭。如果你想看到你的警报,你必须保持空间。

如果您要调试onKeyPress事件,建议您使用console.log而不是警报。 console.log会将输出写入控制台日志。