特殊字符在OSX上的html keypress事件上不起作用 [英] Special characters not working on html keypress events on OSX
问题描述
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< script src =js / jquery.js>< / script>
< / head>
< body>
< script>
$('body')。keypress(function(e){
console.log('char:',String.fromCharCode(e.keyCode));
});
< / script>
< / body>
< / html>尝试输入特殊字符,例如
c $ c>会打印 aeiou
。
keypress
的文档说:注意:作为 keypress
事件不受任何官方规范的约束,在浏览器,浏览器版本和平台上使用时遇到的实际行为可能会有所不同。 它还说:要确定输入了哪个字符,请检查传递给处理函数的事件
对象。虽然浏览器使用不同的属性来存储这些信息,但jQuery对。
属性进行了规范化处理,以便您可以可靠地使用它来检索字符代码。 至少在Windows中,使用 The following page won't work as expected. Trying to type special characters such as Note that jQuery documentation of At least on Windows, using 这篇关于特殊字符在OSX上的html keypress事件上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! e.which
在浏览器中给出一致的结果,而 keydown
和 keyup
(它们应指示按下的键,并且你需要在你的程序逻辑中从它们中推导出字符)。 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script src="js/jquery.js"></script>
</head>
<body>
<script>
$('body').keypress(function(e){
console.log('char: ',String.fromCharCode(e.keyCode));
});
</script>
</body>
</html>
á é í ó ú
will print a e i o u
instead.keypress
says: "Note: as the keypress
event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms." It also says: "To determine which character was entered, examine the event
object that is passed to the handler function. While browsers use differing properties to store this information, jQuery normalizes the .which
property so you can reliably use it to retrieve the character code."e.which
gives consistent results in browsers, whereas e.keyCode
does not. And it yields the character entered, e.g. á when I press the acute key (´) and then the A key. If this does not happen in OSX, then the explanation is probably system-level difference in handling keys; you might need to check whether you can then use keydown
and keyup
more successfully (they should indicate the keys pressed, and you would need to deduce the character from them in your program logic).