如何将char转换为其键码? [英] How to convert a char to its keycode?

查看:116
本文介绍了如何将char转换为其键码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符转换为相应的键码?

How can I convert a character to its respective keycode?

例如:


  • a 65

  • b 66

  • c 67

  • d 68

  • a to 65
  • b to 66
  • c to 67
  • d to 68

推荐答案

您可以使用 charCodeAt 实现此功能。

You can use the charCodeAt function to achieve this.

工作示例:

function showKeyCode () {
    var character = document.getElementById("character").value.substring(0, 1);
    var code = document.getElementById("character").value.charCodeAt(0);
    var msg = "The Key Code for the \"" + character + "\" character is " + code + ".";
    alert(msg);
}

<input type="text" id="character" size="15">
<input type="button" value="Show Key Code" onclick="showKeyCode();">

这篇关于如何将char转换为其键码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆