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

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 16:19:36
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了js實現鍵盤上下左右鍵選擇文字并顯示在文本框的方法,涉及javascript操作鍵盤事件及文本框的相關技巧,非常簡單實用,需要的朋友可以參考下

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

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns="http://www.w3.org/1999/xhtml"
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"
  6. <title>SimulateUpDownKeySelect.html</title> 
  7. <style type="text/css"
  8. #divSelect {border:1px solid red; width:208px !important;width:210px;} 
  9. #divSelect ul {width:200px;margin:3px; margin-left:-35px;*margin-left:3px;overflow:hidden} 
  10. #divSelect ul li {float:left; list-style-type:none;width:45px;height:14px;line-height:20px;font:14px arial;text-align:center;padding:2px} 
  11. #divSelect li:hover {background :green;cursor:pointer} 
  12. #txtInput {width:205px;} 
  13. </style> 
  14. </head> 
  15. <body> 
  16. <form method="post" action="##"
  17. <input type="text" id="txtInput" value="" autocomplete="off" onkeydown="if(event.keyCode==13)return false;" /> 
  18. <!-- 
  19. 防止回車鍵觸發表單提交 
  20. onKeyPress 
  21. --> 
  22. <div id="divSelect"
  23. </div> 
  24. <script type="text/javascript"
  25. var list="<ul>" 
  26. 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>"
  27. list+="</ul>" 
  28. document.getElementById('divSelect').innerHTML=list; 
  29. </script> 
  30. </form> 
  31. <script type="text/javascript"
  32. <!-- 
  33. function $(sId) 
  34. return document.getElementById(sId); 
  35. function clearSelectedOptBgColor(target) 
  36. if (target.seletedIndex >= 0) 
  37. target.options[target.seletedIndex].style.background = ""
  38. function setSelectedOptBgColor(target) 
  39. target.options[target.seletedIndex].style.background = "green"
  40. var upKeyCode = 38; 
  41. var downKeyCode = 40; 
  42. var enterKeyCode = 13; 
  43. var oInput = $("txtInput"); 
  44. oInput.options = $("divSelect").getElementsByTagName("li"); 
  45. oInput.seletedIndex = -1; 
  46. oInput.focus(); 
  47. //oInput.onKeyPress{} 
  48. oInput.onkeyup = function(event){ 
  49. if (event == undefined) 
  50. event = window.event; 
  51. switch (event.keyCode) 
  52. case 37: 
  53. clearSelectedOptBgColor(this); 
  54. this.seletedIndex--; 
  55. if (this.seletedIndex < 0) 
  56. this.seletedIndex = this.options.length - 1; 
  57. this.value = this.options[this.seletedIndex].innerHTML; 
  58. setSelectedOptBgColor(this); 
  59. break
  60. case 38: 
  61. clearSelectedOptBgColor(this); 
  62. this.seletedIndex= this.seletedIndex-4; 
  63. if (this.seletedIndex < 0) 
  64. this.seletedIndex = this.options.length - 1; 
  65. this.value = this.options[this.seletedIndex].innerHTML; 
  66. setSelectedOptBgColor(this); 
  67. break
  68. case 39: 
  69. clearSelectedOptBgColor(this); 
  70. this.seletedIndex++; 
  71. if (this.seletedIndex >= this.options.length) 
  72. this.seletedIndex = 0; 
  73. this.value = this.options[this.seletedIndex].innerHTML; 
  74. setSelectedOptBgColor(this); 
  75. break
  76. case 40: 
  77. clearSelectedOptBgColor(this); 
  78. this.seletedIndex= this.seletedIndex+4; 
  79. if (this.seletedIndex >= this.options.length) 
  80. this.seletedIndex = 0; 
  81. this.value = this.options[this.seletedIndex].innerHTML; 
  82. setSelectedOptBgColor(this); 
  83. break
  84. case enterKeyCode: 
  85. this.value = this.options[this.seletedIndex].innerHTML; 
  86. //alert('aa') 
  87. break
  88. }; 
  89. oInput.onblur = function(){ 
  90. clearSelectedOptBgColor(this); 
  91. this.seletedIndex = 1; 
  92. }; 
  93. //--> 
  94. </script> 
  95. </body> 
  96. </html> 

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 安庆市| 桓仁| 周口市| 江津市| 门源| 广宗县| 中超| 库尔勒市| 财经| 陆河县| 新巴尔虎右旗| 深泽县| 兴宁市| 科技| 通河县| 肥西县| 静安区| 石台县| 牡丹江市| 明光市| 友谊县| 柞水县| 尼木县| 通许县| 奉贤区| 株洲市| 平谷区| 湖口县| 永宁县| 抚州市| 施甸县| 邢台县| 阿瓦提县| 南安市| 潍坊市| 霍州市| 杭州市| 台湾省| 石狮市| 新丰县| 遂昌县|