js实现键盘上下左右键选择文字并显示在文本框的
网络编程 2021-07-04 21:47www.168986.cn编程入门
这篇文章主要介绍了js实现键盘上下左右键选择文字并显示在文本框的方法,涉及javascript操作键盘事件及文本框的相关技巧,非常简单实用,需要的朋友可以参考下
本文实例讲述了js实现键盘上下左右键选择文字并显示在文本框的方法。分享给大家供大家参考。具体实现方法如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://.w3./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>SimulateUpDownKeySelect.html</title> <style type="text/css"> #divSelect {border:1px solid red; width:208px !important;width:210px;} #divSelect ul {width:200px;margin:3px; margin-left:-35px;*margin-left:3px;overflow:hidden} #divSelect ul li {float:left; list-style-type:none;width:45px;height:14px;line-height:20px;font:14px arial;text-align:center;padding:2px} #divSelect li:hover {background :green;cursor:pointer} #txtInput {width:205px;} </style> </head> <body> <form method="post" action="##"> <input type="text" id="txtInput" value="" aulete="off" onkeydown="if(event.keyCode==13)return false;" /> <!-- 防止回车键触发表单提交 onKeyPress --> <div id="divSelect"> </div> <script type="text/javascript"> var list="<ul>" list+="<li>科幻片</li><li>战争片</li><li>动作片</li><li>爱情片</li><li>剧情片</li><li>记录片</li><li>综艺片</li><li>喜剧片</li><li>动画片</li><li>励志片</li><li>恐怖片</li><li>古装片</li><li>电视剧</li><li>读书</li><li >小说</li><li>作品集</li><li>历史</li><li>诗歌</li><li >散文</li><li>军事</li>"; list+="</ul>" document.getElementById('divSelect').innerHTML=list; </script> </form> <script type="text/javascript"> <!-- function $(sId) { return document.getElementById(sId); } function clearSelectedOptBgColor(target) { if (target.seletedIndex >= 0) target.options[target.seletedIndex].style.background = ""; } function setSelectedOptBgColor(target) { target.options[target.seletedIndex].style.background = "green"; } var upKeyCode = 38; var downKeyCode = 40; var enterKeyCode = 13; var oInput = $("txtInput"); oInput.options = $("divSelect").getElementsByTagName("li"); oInput.seletedIndex = -1; oInput.focus(); //oInput.onKeyPress{} oInput.onkeyup = function(event){ if (event == undefined) event = window.event; switch (event.keyCode) { case 37: clearSelectedOptBgColor(this); this.seletedIndex--; if (this.seletedIndex < 0) this.seletedIndex = this.options.length - 1; this.value = this.options[this.seletedIndex].innerHTML; setSelectedOptBgColor(this); break; case 38: clearSelectedOptBgColor(this); this.seletedIndex= this.seletedIndex-4; if (this.seletedIndex < 0) this.seletedIndex = this.options.length - 1; this.value = this.options[this.seletedIndex].innerHTML; setSelectedOptBgColor(this); break; case 39: clearSelectedOptBgColor(this); this.seletedIndex++; if (this.seletedIndex >= this.options.length) this.seletedIndex = 0; this.value = this.options[this.seletedIndex].innerHTML; setSelectedOptBgColor(this); break; case 40: clearSelectedOptBgColor(this); this.seletedIndex= this.seletedIndex+4; if (this.seletedIndex >= this.options.length) this.seletedIndex = 0; this.value = this.options[this.seletedIndex].innerHTML; setSelectedOptBgColor(this); break; case enterKeyCode: this.value = this.options[this.seletedIndex].innerHTML; //alert('aa') break; } }; oInput.onblur = function(){ clearSelectedOptBgColor(this); this.seletedIndex = 1; }; //--> </script> </body> </html>
希望本文所述对大家的javascript程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程