《JavaScript学习笔记》:键盘事件
2016-06-06 20:03
393 查看
《JavaScript学习笔记》:键盘事件
上篇博文介绍了鼠标事件,本篇博文介绍下键盘事件,键盘事件有:键按下(onkeydown)和键弹起(onkeyup).虽然只有键盘只有两种事件,但是,在用途还是相当广的,例子,在我们的qq聊天界面中,我们写好消息好,我们一般有三种方式来进行发送,如下:
1、点击”发送”按钮
2、enter
3、ctrl+enter
后面两种就是键盘事件,前面一种是鼠标点击事件。
要说明的是,当我们不知道某个键的键码时,可以使用keyCode来进行获取。
下面我们就一一进行实现。
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/html; charset=gb2312" /> <title>无标题文档</title> <script> window.onload=function() { var oTxt=document.getElementById('txt1'); var oBtn=document.getElementById('btn1'); var oTxt2=document.getElementById('txt2'); oBtn.onclick=function() { oTxt2.value+=oTxt.value+'\n'; oTxt.value=''; }; }; </script> </head> <body> <textarea id="txt2" rows="20" cols="20"> </textarea> <br /> <input id="txt1" type="text"/> <input id="btn1" type="button" value="发送消息" /> </body> </html>
2、按enter键进行发送
<!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/html; charset=gb2312" /> <title>无标题文档</title> <script> window.onload=function() { var oTxt=document.getElementById('txt1'); var oTxt2=document.getElementById('txt2'); oTxt.onkeydown=function(ev) { var oEvent=ev||event; //alert(oEvent.keyCode); if(oEvent.keyCode==13)//按回车提交,enter的键值为13 { oTxt2.value+=oTxt.value+'\n'; oTxt.value=''; } }; }; </script> </head> <body> <input id="txt1" type="text"/> <br /> <textarea id="txt2" rows="20" cols="20"> </textarea> </body> </html>
3、按enter和ctrl键来进行消息的发送
<!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/html; charset=gb2312" /> <title>无标题文档</title> <script> window.onload=function() { var oTxt = document.getElementById('txt1'); var oTxt2 = document.getElementById('txt2'); oTxt.onkeydown=function(ev) { var oEvent=ev||event; //oEvent.ctrlKey不能写成oEvent.keyCode==17 if(oEvent.keyCode==13&&oEvent.ctrlKey) { oTxt2.value+=oTxt.value+'\n'; oTxt.value=''; } }; }; </script> </head> <body> <input id="txt1" type="text" /> <br /> <textarea id="txt2" rows="20" cols="20"> </textarea> </body> </html>
以上就是三种消息发送方式的实现,都比较简单哈。
例子二:左右移动
下面还看一个例子:用键盘的左右键来控制div的左右移动。相信有了上面的例子,完成这个就相当easy了。
代码如下:
<!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/html; charset=gb2312" /> <title>无标题文档</title> <style> #div1 {width:200px;height:200px;background:red;position:absolute} </style> <script> window.onload=function() { var oDiv = document.getElementById('div1'); document.onkeydown=function(ev) { var oEvent=ev||event; //alert(ev.keyCode); if(ev.keyCode==37)//向左 { oDiv.style.left=oDiv.offsetLeft-10+'px'; } else if(ev.keyCode==39)//向右 { oDiv.style.left=oDiv.offsetLeft+10+'px'; } }; }; </script> </head> <body> <div id="div1"> </div> </body> </html>
相关文章推荐
- 《JavaScript学习笔记》:获取鼠标的位置及应用
- 使用JavaScript实现一个倒数计时程序
- ie 浏览器js关闭窗口弹出警告
- 一个简单的基于JSTL的分页
- 【JavaScript】——为静态网页插上了翅膀
- Date UTC timezone在js json web server中关系探究
- 较之java代码在jsp中
- [Effective JavaScript 笔记]第27条:使用闭包而不是字符串来封装代码
- javascript基础语法——表达式
- js 中this 的陷阱
- 十进制、十六进制转换(javascript实现)
- JavaScript判断数组重复内容的两种方法(推荐)
- jsoup:解析HTML用法小结2
- JavaScript基础精讲
- JavaScript基础精讲
- JavaScript基础精讲
- IOS5中的Safari不兼容Javascript中的Date问题
- JavaScript数组的定义及数字操作技巧
- js笔记---作用域(执行上下文[execution context],活动对象) 闭包
- 跟着9张思维导图学习Javascript