1

キーコードをチェックする関数をjavascriptで書くのに疲れました。Firefox では正常に動作しますが、IE では正常に動作しません。私のコードで何が問題になっているのか誰か知っていますか? 詳細については、以下のコードを参照してください。

function textCheck(e)
{
 var e = window.event || e
 alert("CharCode value: "+e.charCode)
 alert("Character: "+String.fromCharCode(e.charCode))
}
4

2 に答える 2

3

コードの最初の行を切り替えます。

...

var e = e || window.event;

...

そして、それが役立つかどうかを確認してください。

また、Unicode 文字の場合は、次のようなものを試してください。

function displayunicode(e) {
    var unicode=e.keyCode? e.keyCode : e.charCode
    alert(unicode)
}

keydownの代わりに処理している場合keypress、事態はもう少し複雑になります...参照: http://unixpapa.com/js/key.html

于 2009-05-08T01:07:01.047 に答える
1

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 compare 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.com/WestwindWebToolkit/samples/Ajax/html5andCss3/keycodechecker.aspx

Makes it real easy to experiment with different key combinations and see how they compare on different browsers. jQuery's which and keypress tend to be the one that works most consistently.

于 2012-02-13T19:50:10.263 に答える