首页 > 语言 > JavaScript > 正文

jQuery插件datalist实现很好看的input下拉列表

2024-05-06 16:23:07
字体:大 中 小
来源:转载
供稿:网友

本文给大家分享的是使用jQuery实现的房HTML5中的一个好看的input框很好看的下拉列表--datalist,兼容性非常不错,这里推荐给大家,有需要的小伙伴可以参考下。

HTML5中定义了一种input框很好看的下拉列表--datalist,然而目前它的支持性并不好(万恶的IE,好在你要渐渐退役了...)。于是最近更据需求写了一个小型datalist插件,兼容到IE8(IE7应该没多少人用了吧?)。实现的具体需求如下:

当被选中的时候(触发blur焦点)(不管是鼠标还是tab键)清空input框并且显示自定义的下拉列表,然后可以用键盘的上下键选择(鼠标当然肯定没理由不可以啦),单击鼠标左键或者enter键将选中的列表的值输入到input框。

具体的实现代码如下:

HTML

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <meta name="description" content="" /> 
  6. <meta name="keywords" content="" /> 
  7. <meta name="robots" content="index, follow" /> 
  8. <meta name="googlebot" content="index, follow" /> 
  9. <meta name="author" content="codetker" /> 
  10. <title> 表单选中弹出框</title> 
  11. <link href="css/reset.css" type="text/css" rel="Stylesheet" />  
  12. <link href="css/master.css" type="text/css" rel="Stylesheet" />  
  13.  
  14. <script type="text/javascript" src="js/jquery-1.11.0.js"></script> 
  15. </head> 
  16.  
  17. <body> 
  18. <div class="wrap"> 
  19. <form class="center"> 
  20. <div class="input_wrap"> 
  21. <input name="input1" class="input input1" type="text"/> 
  22. <ul class="input1_ul select_list"> 
  23. <li>问题1</li> 
  24. <li>问题2</li> 
  25. <li>问题3</li> 
  26. <li>问题4</li> 
  27. <li>问题5</li> 
  28. </ul> 
  29. </div> 
  30. </form> 
  31. </div> 
  32. <script type="text/javascript" src="js/jquery.codetker.datalist.js"></script> 
  33. <script type="text/javascript"> 
  34. $(document).ready(function(){ 
  35. $(".input_wrap").myDatalist({"bgcolor":"red","widths":1,"heights":1});  
  36. }); 
  37. </script> 
  38. </body> 
  39. </html> 

CSS(reset.css里面是初始化浏览器默认值用的,这里是style.css)

 

 
  1. .wrap{ margin:0 auto; font-family: "微软雅黑";font-size: 14px;} 
  2. .center{ margin: 0 auto; width:500px;} 
  3. .input{ margin: 0; padding: 0; /*border:none;*/ width:140px; height: 24px; float:left;} 
  4. .select_list{display: none; position:absolute; z-index: 100;} 
  5. .select_list li{ height:24px; margin: 0; padding: 0; background-color: #fff; cursor: pointer; list-style: none; position:relative;} 
  6. .select_list li:hover{ background-color: red;} 
  7. .input_wrap{ position:relative; } 

JavaScript

 

 
  1. /* 
  2. datalist 0.1  
  3. 自定义datalist插件,实现html5中input元素datalist的效果 
  4. 兼容IE8+,Firefox,Chrome等常见浏览器 
  5. */ 
  6.  
  7. ;(function($,window,document,undefined){ //undefinde是真实的undefined,并非参数 
  8. //将可选择的变量传递给方法 
  9.  
  10. //定义构造函数 
  11. var Datalist=function(ele,opt){ 
  12. this.$element=ele; 
  13. this.defaults={ 
  14. 'bgcolor':'green', 
  15. 'widths':1, 
  16. 'heights':1 
  17. }, 
  18. this.options=$.extend({}, this.defaults, opt); 
  19. } 
  20. //定义方法 
  21. Datalist.prototype={ 
  22. showList:function(){ 
  23. var color=this.options.bgcolor; 
  24. var width=this.options.widths; 
  25. var height=this.options.heights; //属性值 
  26.  
  27. var obj=this.$element; //obj为最外层包裹的div之类的元素,应该拥有positive:relative属性,方便datalist定位。 
  28. var input=$(obj).children().eq(0); //input元素 
  29. var inputUl=$(obj).children().eq(1); //datalist元素 
  30. //设置弹出datalist的大小和样式 
  31. $(inputUl).css({ 
  32. "top":$(input).outerHeight()+"px", 
  33. "width":$(input).outerWidth()*width+"px" 
  34. }); 
  35. $(inputUl).children().css({ 
  36. "width":$(input).outerWidth()*width+"px", 
  37. "height":$(input).outerHeight()*height+"px" 
  38. }); 
  39.  
  40. $(inputUl).children('li').mouseover(function() { 
  41. $(this).css("background-color",color); 
  42. $(this).siblings().css("background-color","#fff"); 
  43. }); 
  44. $(inputUl).children('li').mouseout(function() { 
  45. $(this).css("background-color","#fff"); 
  46. }); 
  47. //再次focus变为空,也可以改为某个默认值 
  48. //datalist的显示和隐藏 
  49. $(input).focus(function() { 
  50. if($(this).val()!=""){ 
  51. $(this).val(""); 
  52. } 
  53. $(inputUl).slideDown(500); 
  54.  
  55. var n=-1; //记录位置,-1表示未选中。当n=-1时直接按enter浏览器默认为倒数第一个 
  56. $(document).keydown(function(event) { 
  57. /* 点击键盘上下键,datalist变化 */ 
  58. stopDefaultAndBubble(event); 
  59. if(event.keyCode==38){//向上按钮 
  60. if(n==0||n==-1){ 
  61. n=4; 
  62. }else{ 
  63. n--; 
  64. } 
  65. $(inputUl).children('li').eq(n).siblings().mouseout(); 
  66. $(inputUl).children('li').eq(n).mouseover(); 
  67. }else if(event.keyCode==40){//向上按钮 
  68. if(n==4){ 
  69. n=0; 
  70. }else{ 
  71. n++; 
  72. } 
  73. $(inputUl).children('li').eq(n).siblings().mouseout(); 
  74. $(inputUl).children('li').eq(n).mouseover(); 
  75. }else if(event.keyCode==13){//enter键 
  76. $(inputUl).children('li').eq(n).mouseout(); 
  77. $(input).val( $(inputUl).children('li').eq(n).text() ); 
  78. n=-1; 
  79. } 
  80. }); 
  81.  
  82.  
  83. //去掉浏览器默认 
  84. function stopDefaultAndBubble(e){ 
  85. e=e||window.event; 
  86. //阻止默认行为 
  87. if (e.preventDefault) { 
  88. e.preventDefault(); 
  89. } 
  90. e.returnValue=false; 
  91.  
  92. //阻止冒泡 
  93. if (e.stopPropagation) { 
  94. e.stopPropagation(); 
  95. } 
  96. e.cancelBubble=true; 
  97. } 
  98.  
  99. }); 
  100. $(input).blur(function() { 
  101. $(inputUl).slideUp(500); 
  102. }); 
  103. $(inputUl).delegate('li', 'click', function() { 
  104. $(input).val( $(this).text() ); 
  105. }); 
  106.  
  107. return this; 
  108. } 
  109. } 
  110. //在插件中使用Datalist对象 
  111. $.fn.myDatalist=function(options){ 
  112. //创建实体 
  113. var datalist=new Datalist(this,options); 
  114. //调用其方法 
  115. return datalist.showList(); 
  116. } 
  117.  
  118. })(jQuery,window,document); 

这里用ul li列表模拟datalist options。实现逻辑非常简单,稍微需要注意点的是div.input_wrap是用相对定位的,方便ul.input1_ul相对进行定位。由于需求有很多的输入框且相互之间不影响,因此这里是input1。好歹是我自己开发的第一个插件,mark一记。

需要代码的可以戳https://github.com/codetker/myDatalist。

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选