标签:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>keyCode属性-蚂蚁部落</title> <script type="text/javascript"> /*function keyEvent(ev){ var ev=ev||window.event; alert(ev.keyCode); } document.onkeydown=keyEvent; */ function keyEvent(ev){ var ev=ev||window.event; alert(ev.charCode); } document.onkeypress=keyEvent; //火狐下onkeypress a-------97 ,onkeydown a --------0 //火狐下onkeypress b-------98 ,onkeydown b --------0 var getCharCode = function(ev){ var ev=ev||window.event; if (typeof ev.charCode == "number"){ alert(ev.charCode); return ev.charCode; } else { alert(ev.keyCode); return ev.keyCode; } } document.onkeydown=getCharCode; //a--------65 //b--------66 </script> </head> <body> </body> </html>
由于浏览器差异键值输出差异,给出个兼容获取键值的方法
var getCharCode = function(ev){
var ev=ev||window.event;
if (typeof ev.charCode == "number"){
//alert(ev.charCode);
return ev.charCode;
} else {
//alert(ev.keyCode);
return ev.keyCode;
}
}
标签:
原文地址:http://www.cnblogs.com/double405/p/5120384.html