admin管理员组文章数量:1401122
I tried to write a function to check key code with JavaScript. It's working fine for Firefox but not IE. Does anyone know what is going wrong with my code? Please see below code for more details:
function textCheck(e)
{
var e = window.event || e
alert("CharCode value: "+e.charCode)
alert("Character: "+String.fromCharCode(e.charCode))
}
I tried to write a function to check key code with JavaScript. It's working fine for Firefox but not IE. Does anyone know what is going wrong with my code? Please see below code for more details:
function textCheck(e)
{
var e = window.event || e
alert("CharCode value: "+e.charCode)
alert("Character: "+String.fromCharCode(e.charCode))
}
Share
Improve this question
edited Jan 21, 2023 at 0:08
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked May 8, 2009 at 1:04
Jin YongJin Yong
43.8k72 gold badges144 silver badges194 bronze badges
1
- 1 Please add your call site so we can see how you are calling this function. – Adam Markowitz Commented May 8, 2009 at 1:09
2 Answers
Reset to default 3switch your first line of code:
...
var e = e || window.event;
...
and see if that helps.
Also, if it is a unicode character try something like the following:
function displayunicode(e) {
var unicode=e.keyCode? e.keyCode : e.charCode
alert(unicode)
}
If you're handling keydown
instead of keypress
, then things get a bit more plex... See: http://unixpapa./js/key.html
keyCode and charCode values behave slightly differently in different browsers and return different values for various events. Older versions of IE in particular don't support charCode. The first suggestion I have is to use a tool like jQuery to handle normalization of the key values across browsers. With jQuery you can use the e.which property on the event object to get the normalized value.
The other thing to do is use a key code checker to pare the values returned for each event for each browser. I built a handy online tool to do this and found it extremely useful to show all the keycodes/charcodes for each of the events at a glance:
http://www.west-wind./WestwindWebToolkit/samples/Ajax/html5andCss3/keycodechecker.aspx
Makes it real easy to experiment with different key binations and see how they pare on different browsers. jQuery's which and keypress tend to be the one that works most consistently.
本文标签: dom eventsKey code check in JavaScript not working for IEStack Overflow
版权声明:本文标题:dom events - Key code check in JavaScript not working for IE - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744291322a2599126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论