【发布时间】:2018-01-16 19:38:14
【问题描述】:
我正在尝试创建一个 UI,允许用户按 Enter 键以通过大多数屏幕。这在大多数浏览器中效果很好,但在 IE 中似乎失败了。
我设置了一个显示行为的 JS Fiddle -- https://jsfiddle.net/fat9qy40/2/。隐藏某种形式的元素似乎很重要。在我的实际用例中,我弹出一个带有一些说明的 div,并希望用户能够按 Enter 键将其关闭——该 div 下方仍有一个底层表单。
HTML:
<html>
<head>
</head>
<body>
<button>
Fake button
</button>
<div style="z-index:100;position:absolute;top:0;left:0;border:1px solid black;background-color:green;height:50px;padding:10px">
Press enter to see an alert (unless you're using IE!)
</div>
</body>
</html>
JS:
$(document).on('keypress', function(e){
alert(e.which)
})
在 Chrome 中,如果我单击右下角的空白区域,然后按 Enter,我会弹出一个显示“13”的弹出窗口,这正是我所期望的。在 IE 11 中,如果我做同样的事情,什么也不会发生。但是,其他按键确实可以正确触发按键事件;例如,如果我按 F,我会收到一条显示“102”的警报。
任何人有任何解决方法或建议?谢谢!
【问题讨论】:
-
看起来这已在 Edge 中修复,但他们并未将其反向移植到 IE。 connect.microsoft.com/IE/feedback/details/1552272
-
如果您使用 keydown 或 keyup 代替?
标签: javascript jquery internet-explorer keypress enter