js中鍵盤事件實例簡析
更新時間:2015年01月10日 16:25:36 投稿:shichen2014
這篇文章主要介紹了js中鍵盤事件,以一個較為簡單的實例形式分析了js響應鍵盤事件的操作技巧,需要的朋友可以參考下
本文實例分析了js中鍵盤事件。分享給大家供大家參考。具體分析如下:
該實例效果:
按鍵盤上的任意一個鍵,彈出相應的ASCII碼,兼容ie,chrome和firefox。
但還是有不少問題:
(1)ie和chrome中,一些鍵沒有效果,如上、下、左、右等;
(2)而firefox中的向右鍵,與單引號鍵,都為39。
具體代碼如下:
復制代碼 代碼如下:
<html>
<head>
<script type="text/javascript">
window.onload = function(){
var bd = document.getElementsByTagName('body')[0];
bd.onkeypress = function(ev){
ev = ev || window.event;//ie不支持function參數(shù)ev
alert(ev.keyCode || ev.which);//火狐不支持keyCode
}
}
</script>
<style type="text/css">
#par{width:300px;height:200px;background:gray;}
#son{width:200px;height:100px;background:green;}
</style>
</head>
<body>
<div id="par">
<div id="son"></div>
</div>
</body>
</html>
<head>
<script type="text/javascript">
window.onload = function(){
var bd = document.getElementsByTagName('body')[0];
bd.onkeypress = function(ev){
ev = ev || window.event;//ie不支持function參數(shù)ev
alert(ev.keyCode || ev.which);//火狐不支持keyCode
}
}
</script>
<style type="text/css">
#par{width:300px;height:200px;background:gray;}
#son{width:200px;height:100px;background:green;}
</style>
</head>
<body>
<div id="par">
<div id="son"></div>
</div>
</body>
</html>
希望本文所述對大家的javascript程序設計有所幫助。
相關文章
javascript html5 canvas實現(xiàn)可拖動省份的中國地圖
這篇文章主要介紹了javascript html5 canvas實現(xiàn)可拖動省份的中國地圖的相關資料,需要的朋友可以參考下2016-03-03

