admin管理员组文章数量:1291044
<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,
and it always alert UpperCase of a charcode.
<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,
and it always alert UpperCase of a charcode.
Share Improve this question edited Feb 15, 2010 at 3:12 Asaph 163k25 gold badges203 silver badges204 bronze badges asked Feb 15, 2010 at 3:10 zjm1126zjm1126 66.8k86 gold badges179 silver badges222 bronze badges2 Answers
Reset to default 9At 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.
From this link: http://www.javascriptkit./jsref/eventkeyboardmouse.shtml
Be careful when accessing the keyCode property during an onkeydown or onkeyup event, as it is set whenever any key is pressed, including non character keys like "Shift". This means if you try to press "Shift+a" to try and get the keyCode for "A", you will always end up getting two keyCodes instead, one for "Shift" and one for "A" in that order. What you won't get regardless is the keyCode for "a", as keyCode always returns the unicode value of the uppercase version of a character
- 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 posite value of one or more keys being pressed in unison.keydown
andkeyup
return the value of a single key while ignoring any key binations.
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?
(guess)
It's referencing the Key which doesn't have a case. Case is determined by whether or not the shift key is also down.
本文标签: javascriptWhy always UpperCase in my codeStack Overflow
版权声明:本文标题:javascript - Why always UpperCase in my code? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507045a2382383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论