js中如何得到从键盘上输入的字符,请问是用哪个方法

JavaScript011

js中如何得到从键盘上输入的字符,请问是用哪个方法,第1张

只能得到编码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无标题文档</title>

<script>

function a(e){

var ent=window.event|e

if(document.all){

alert(e.keyCode)

}

else{

alert(e.which)

}

}

</script>

</head>

<body>

<input type="text" onkeypress="a(event)" />

</body>

</html>

通过key值可以查到是哪个键

document.onkeydown=function(e){console.log(e.keyCode)}

监听键盘事件,回车是13,空格是32;

你可以复制上面的代码,按F12 粘贴进控制台,按键盘就可以看到每个键对应的code