国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js實現鍵盤上下左右鍵選擇文字并顯示在文本框的方法

2019-11-20 12:32:05
字體:
來源:轉載
供稿:網友

本文實例講述了js實現鍵盤上下左右鍵選擇文字并顯示在文本框的方法。分享給大家供大家參考。具體實現方法如下:

<!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=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="" autocomplete="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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北辰区| 深泽县| 天祝| 调兵山市| 龙川县| 苍山县| 靖安县| 卓尼县| 乐亭县| 台中市| 监利县| 潮州市| 宁阳县| 绥滨县| 花垣县| 崇左市| 收藏| 镇巴县| 湟源县| 垣曲县| 东至县| 龙州县| 华蓥市| 原平市| 井冈山市| 濮阳市| 确山县| 开阳县| 英吉沙县| 鸡东县| 珠海市| 墨脱县| 上饶县| 大同县| 当涂县| 文登市| 陆川县| 高邮市| 乌什县| 潍坊市| 双桥区|