问题描述
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<input id=a type="text" value='sss'/>
<script type="text/javascript">
$('#a').keyup(
function(event){
alert(String.fromCharCode(event.which))
})
</script>
您可以在浏览器中测试此代码,
you can test this code in your brower,
它总是提醒TopCase一个charcode。
and it always alert UpperCase of a charcode.
推荐答案
起初我认为这是一个bug,因为预期的小写返回 keypress
事件的值。事实证明,在 keyup / keydown
上,始终返回密钥的ASCII大写/非移位版本。
At first I thought this was a bug, as the expected lowercase value is returned for the keypress
event. It turns out that on keyup/keydown
the ASCII uppercase/non-shifted version of a key is always returned.
从以下链接:
- 按Shift + a算作单个
按键
event。 - 当按下键时,按Shift + a计为两个
keydown
事件,两个keyup
释放密钥时的事件。 -
keypress
返回复合一致按下一个或多个键的值。 -
keydown
和keyup
返回单键的值,同时忽略任何组合键。 - Pressing Shift+a counts as a single
keypress
event. - Pressing Shift+a counts as two
keydown
events when the keys are pressed down and twokeyup
event when the keys are released. keypress
returns the composite value of one or more keys being pressed in unison.keydown
andkeyup
return the value of a single key while ignoring any key combinations.
现在这里有一个令人困惑的部分:由于某种原因,的未移位值
key作为键代码65返回。但是65表是ASCII表中的大写A.所以沿着这条线的某个地方,浏览器采用小写字母a(ASCII代码97),将其转换为大写字母A,然后将 keydown / keyup
作为非移位字符传递。很奇怪,不是吗?
Now here's the confusing part: for some reason the unshifted value of the a
key is returned as key code 65. But 65 is uppercase A in the ASCII table. So somewhere along the line the browser is taking the lowercase a (ASCII code 97), transforming it to uppercase A, then passing it keydown/keyup
as the non-shifted character. Weird, isn't it?
这篇关于为什么我的代码中始终使用UpperCase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!