使用jQuery的keyPressed,IE8中的EnterKey有时不起作用 [英] EnterKey is not working sometimes in IE8, using jQuery's keyPressed
问题描述
在我的HTML
css中:
In my HTML css:
.edit_field {
height: 50px;
width: 495px;
line-height: 3.6;
}
html:
<textarea id="message" class='edit_field'>Enter message here</textarea>
javascrpt:
javascrpt:
$('#message').keypress(function(event) {
// not getting fired this block when I pressed enter key
// For all other keys its got fired well.
if(event.which == 13) {
// -- -- --
}
});
我的代码或浏览器(IE8)有问题吗?
Is there any problem in my code or in my browser(IE8)?
推荐答案
试试这个:
$('#message').keyup(function(e){
if(e.keyCode == 13){
//your code probably want e.preventDefault() as well
}
});
我发现按键实现不是那么跨浏览器友好。只是一个想法。
I've found keypress implementation to not be so cross-browser friendly. Just a thought.
编辑:
我现在记得为什么这在IE8中不起作用。输入键(键码13)被认为是特殊键(以及其他类似键盘上的箭头键)IE不会触发特殊键的按键事件,这就是我必须使用键盘的原因。
Edit: I now remember why this doesn't work in IE8. The enter key (keycode 13) is considered a "Special Key" (as well as others like arrow keys on the keyboard) IE does NOT fire a keypress event for "Special Keys" which is why I had to use keyup.
您可以在此处找到更多信息: http: //www.quirksmode.org/dom/events/keys.html
You can find more info here: http://www.quirksmode.org/dom/events/keys.html
查看特殊密钥
这篇关于使用jQuery的keyPressed,IE8中的EnterKey有时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!