DIV CSS 佈局教程網

自製 jQuery datalist 外掛,做出漂亮的輸入框下拉列表
編輯:關於JavaScript     

HTML5 定義了 datalist 這個很方便的輸入框下拉提示,但瀏覽器支援度一直不太理想(尤其舊 IE)。因此乾脆自己寫一個小型外掛來補足,相容到 IE8,功能對齊原生 datalist。

需求

輸入框取得焦點時(不論滑鼠點擊或 Tab 鍵),先清空內容並展開自製的下拉列表;接著可以用上下鍵或滑鼠游標選擇;按 Enter 或單擊左鍵就把選中項帶回輸入框。

HTML

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="description" content="" /> <meta name="keywords" content="" /> <meta name="robots" content="index, follow" /> <meta name="googlebot" content="index, follow" /> <meta name="author" content="codetker" /> <title> 表單選中彈出框</title> <link href="css/reset.css" type="text/css" rel="Stylesheet" /> <link href="css/master.css" type="text/css" rel="Stylesheet" /> <script type="text/javascript" src="js/jquery-1.11.0.js"></script> </head> <body> <div class="wrap"> <form class="center"> <div class="input_wrap"> <input name="input1" class="input input1" type="text"/> <ul class="input1_ul select_list"> <li>問題1</li> <li>問題2</li> <li>問題3</li> <li>問題4</li> <li>問題5</li> </ul> </div> </form> </div> <script type="text/javascript" src="js/jquery.codetker.datalist.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".input_wrap").myDatalist({"bgcolor":"red","widths":1,"heights":1}); }); </script> </body> </html> 

CSS

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

這裡有個關鍵:.input_wrap 必須是 position: relative,這樣 .select_list 的絕對定位才會以它為基準,下拉框才能精準貼在輸入框下方。

JavaScript

 /* datalist 0.1 自定義datalist插件,實現html5中input元素datalist的效果 兼容IE8+,Firefox,Chrome等常見浏覽器 */ ;(function($,window,document,undefined){ //undefinde是真實的undefined,並非參數 //將可選擇的變量傳遞給方法 //定義構造函數 var Datalist=function(ele,opt){ this.$element=ele; this.defaults={ 'bgcolor':'green', 'widths':1, 'heights':1 }, this.options=$.extend({}, this.defaults, opt); } //定義方法 Datalist.prototype={ showList:function(){ var color=this.options.bgcolor; var width=this.options.widths; var height=this.options.heights; //屬性值 var obj=this.$element; //obj為最外層包裹的div之類的元素,應該擁有positive:relative屬性,方便datalist定位。 var input=$(obj).children().eq(0); //input元素 var inputUl=$(obj).children().eq(1); //datalist元素 //設置彈出datalist的大小和樣式 $(inputUl).css({ "top":$(input).outerHeight()+"px", "width":$(input).outerWidth()*width+"px" }); $(inputUl).children().css({ "width":$(input).outerWidth()*width+"px", "height":$(input).outerHeight()*height+"px" }); $(inputUl).children('li').mouseover(function() { $(this).css("background-color",color); $(this).siblings().css("background-color","#fff"); }); $(inputUl).children('li').mouseout(function() { $(this).css("background-color","#fff"); }); //再次focus變為空,也可以改為某個默認值 //datalist的顯示和隱藏 $(input).focus(function() { if($(this).val()!=""){ $(this).val(""); } $(inputUl).slideDown(500); var n=-1; //記錄位置,-1表示未選中。當n=-1時直接按enter浏覽器默認為倒數第一個 $(document).keydown(function(event) { /* 點擊鍵盤上下鍵,datalist變化 */ stopDefaultAndBubble(event); if(event.keyCode==38){//向上按鈕 if(n==0||n==-1){ n=4; }else{ n--; } $(inputUl).children('li').eq(n).siblings().mouseout(); $(inputUl).children('li').eq(n).mouseover(); }else if(event.keyCode==40){//向上按鈕 if(n==4){ n=0; }else{ n++; } $(inputUl).children('li').eq(n).siblings().mouseout(); $(inputUl).children('li').eq(n).mouseover(); }else if(event.keyCode==13){//enter鍵 $(inputUl).children('li').eq(n).mouseout(); $(input).val( $(inputUl).children('li').eq(n).text() ); n=-1; } }); //去掉浏覽器默認 function stopDefaultAndBubble(e){ e=e||window.event; //阻止默認行為 if (e.preventDefault) { e.preventDefault(); } e.returnValue=false; //阻止冒泡 if (e.stopPropagation) { e.stopPropagation(); } e.cancelBubble=true; } }); $(input).blur(function() { $(inputUl).slideUp(500); }); $(inputUl).delegate('li', 'click', function() { $(input).val( $(this).text() ); }); return this; } } //在插件中使用Datalist對象 $.fn.myDatalist=function(options){ //創建實體 var datalist=new Datalist(this,options); //調用其方法 return datalist.showList(); } })(jQuery,window,document); 

幾個實作重點

用 n = -1 表示「尚未選中」。 上下鍵在邊界會循環(0 往上跳到最後一項、最後一項往下跳回 0),這個設計讓鍵盤操作不會卡死。

阻止上下鍵的預設行為。 否則按方向鍵會連帶捲動整個頁面。stopDefaultAndBubble 同時處理 preventDefault 與 stopPropagation,兼顧新式與舊式瀏覽器。

用 mouseover/mouseout 模擬選中態。 因為鍵盤移動時也要套用同一套視覺回饋,直接呼叫 mouseover() 比另外寫一個 highlight 函式更快,但缺點是語意不夠清楚,維護時值得重構。

作者也自fully 表示這是自己寫的第一個外掛,值得記上一筆;原始碼在 GitHub 的 myDatalist 專案。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved