基于jquery實(shí)現(xiàn)的可編輯下拉框?qū)崿F(xiàn)代碼
原理就是一個(gè)textbox加一個(gè)ul模擬下拉框,用font模擬一個(gè)下拉按鈕。
一、制作靜態(tài)效果
先用css和html,做出一個(gè)應(yīng)該有的樣子。這里這兩個(gè)我使用的是字體,可以在icomoon網(wǎng)站上面自己制作。用字體的好處是和輸入框定位很方便,而且還可以控制大小顏色等,唯一的不足是IE6和IE7由于不支持:before選擇器,導(dǎo)致無法顯示這種字體,但可以通過一些其他方法實(shí)現(xiàn),大家可以自己嘗試下。下面是html代碼
<span style="display:inline-block;position:relative" class="combox_border"> <input type="text" class="combox_input"/><font class="ficomoon icon-angle-bottom combox_button" style="display:inline-block"></font> <ul style="position:absolute;top:29px;left:-1px" class="combox_select"> <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >選項(xiàng)一</a></li> <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >選項(xiàng)二</a></li> <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >選項(xiàng)三</a></li> <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >選項(xiàng)四</a></li> </ul> </span>
1、標(biāo)簽中有style和class,這個(gè)style就是必須屬性,一定要有
2、最外圍是用span來做包裹的,然后給了個(gè)inline-block屬性,之所以用行內(nèi)元素是為了以后布局的方便,換成塊元素也是可以的,但很多時(shí)候塊元素會(huì)伴隨著float浮動(dòng)等樣式,控制起來比較麻煩
3、ficomoon icon-angle-bottom在定義字體
4、span的屬性position是relative,下拉我就打算用ul定位來模擬,ul的position是absolute,top以后可以根據(jù)jquery來獲取span的高度設(shè)置,left就寫死了
5、li里面的內(nèi)容我加了個(gè)a標(biāo)簽,這里就是想偷懶一下,a標(biāo)簽有個(gè):hover偽類,移上去可以改變CSS,這樣我就能少寫這個(gè)移到內(nèi)容上去變樣式的特效
下面是CSS代碼:
@font-face { font-family: 'icomoon'; src:url('fonts/icomoon.eot?-fl11l'); src:url('fonts/icomoon.eot?#iefix-fl11l') format('embedded-opentype'), url('fonts/icomoon.woff?-fl11l') format('woff'), url('fonts/icomoon.ttf?-fl11l') format('truetype'), url('fonts/icomoon.svg?-fl11l#icomoon') format('svg'); font-weight: normal; font-style: normal; } .ficomoon{font-family:'icomoon';} .icon-angle-top:before {content: "\f102"}.icon-angle-bottom:before {content: "\f103"} /*下面的可根據(jù)自己的實(shí)際情況做修改*/ .combox_border{border:1px solid #c2c2c2;height:28px;width:245px} .combox_input{border:0;line-height:25px;height:25px;padding-left: 5px;width:85%;vertical-align: middle;} .combox_button{width:12%;text-align:center;vertical-align: middle;cursor:pointer;border-left:1px solid #c2c2c2} .combox_select{border:1px solid #c2c2c2;border-top:0;width:100%} .combox_select li{overflow:hidden;height:30px;line-height:30px;cursor:pointer;} .combox_select a {display: block;line-height: 28px;padding: 0 8px;text-decoration: none;color: #666;} .combox_select a:hover {text-decoration: none;background:#f5f5f5}
這里的combox_border等樣式可以自定義,可以加CSS3樣式美化,我這里就做了個(gè)樸素的樣式。
二、制作JS特效
在做JS的時(shí)候,碰到個(gè)奇怪的問題,就是放在任何瀏覽器中都不會(huì)報(bào)錯(cuò),但是在IE6死活提示未設(shè)置的對(duì)象屬性的錯(cuò)誤,最后發(fā)現(xiàn)是因?yàn)閖s文件編碼的問題,不是UTF-8,改變下編碼就可以了。
先是一個(gè)jquery插件格式
(function($){ $.fn.combox = function(options) { }; })(jQuery);
然后是添加默認(rèn)參數(shù)
var defaults = { borderCss: "combox_border", inputCss: "combox_input", buttonCss: "combox_button", selectCss: "combox_select", datas:[] }; var options = $.extend(defaults, options);
borderCss | 最外面包裹的樣式,就是上面的span |
inputCss | 輸入框的樣式 |
buttonCss | 按鈕的樣式,就是 |
selectCss | 下拉列表的樣式 |
datas | 下拉列表中的內(nèi)容 |
然后是一個(gè)渲染的方法
this.each(function() { var _this = $(this); _this = _initBorder(_this);//初始化外框CSS IE6中需要有返回值 _this = _initInput(_this);//初始化輸入框 _initSelect(_this);//初始化下拉列表 });
動(dòng)態(tài)的生成輸入框,按鈕,下拉框,附上樣式和時(shí)間。我將三個(gè)渲染分別放在了三個(gè)函數(shù)中,這樣清晰一點(diǎn)
function _initBorder($border) {//初始化外框CSS $border.css({'display':'inline-block', 'position':'relative'}).addClass(options.borderCss); return $border; } function _initInput($border){//初始化輸入框 $border.append('<input type="text" class="'+options.inputCss+'"/>'); $border.append('<font class="ficomoon icon-angle-bottom '+options.buttonCss+'" style="display:inline-block"></font>'); //綁定下拉特效 $border.delegate('font', 'click', function() { var $ul = $border.children('ul'); if($ul.css('display') == 'none') { $ul.slideDown('fast'); $(this).removeClass('icon-angle-bottom').addClass('icon-angle-top'); }else { $ul.slideUp('fast'); $(this).removeClass('icon-angle-top').addClass('icon-angle-bottom'); } }); return $border;//IE6需要返回值 } function _initSelect($border) {//初始化下拉列表 $border.append('<ul style="position:absolute;left:-1px;display:none" class="'+options.selectCss+'">'); var $ul = $border.children('ul'); $ul.css('top',$border.height()+1); var length = options.datas.length; for(var i=0; i<length ;i++) $ul.append('<li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+options.datas[i]+'</a></li>'); $ul.delegate('li', 'click', function() { $border.children(':text').val($(this).text()); $ul.hide(); $border.children('font').removeClass('icon-angle-top').addClass('icon-angle-bottom');//確定的時(shí)候要將下拉的icon改變 }); return $border; }
三個(gè)函數(shù)中的參數(shù)我都加了個(gè)$符號(hào),便于自己知道這是一個(gè)jquery對(duì)象。這幾個(gè)函數(shù)中沒什么技術(shù)難點(diǎn),都是非常普通自然的邏輯,大家也可以隨時(shí)根據(jù)自己的不同需求改變代碼,插件總共只有50幾行,非常容易修改。
下面是調(diào)用插件:
<script type="text/javascript"> $(document).ready(function() { $('#combox').combox({datas:['選項(xiàng)一','選項(xiàng)二','選項(xiàng)三']}); }) </script> </head> <body> <span id="combox"></span> </body> </html>
一句話就可以了,挺方便的。
demo演示:http://demo.jb51.net/js/2014/combox_jquery/
demo下載:http://chabaoo.cn/jiaoben/199034.html
- 基于jQuery的select下拉框選擇觸發(fā)事件實(shí)例分析
- jquery中dom操作和事件的實(shí)例學(xué)習(xí) 下拉框應(yīng)用
- jQuery操作select下拉框的text值和value值的方法
- jquery獲得下拉框值的代碼
- jquery及原生js獲取select下拉框選中的值示例
- jquery動(dòng)態(tài)加載select下拉框示例代碼
- 利用jquery實(shí)現(xiàn)下拉框的禁用與啟用
- 用Jquery實(shí)現(xiàn)多級(jí)下拉框無刷新的聯(lián)動(dòng)
- jQuery實(shí)現(xiàn)監(jiān)聽下拉框選中內(nèi)容發(fā)生改變操作示例
相關(guān)文章
JQuery對(duì)表單元素的基本操作使用總結(jié)
這篇文章主要介紹了JQuery對(duì)表單元素的基本操作使用總結(jié),需要的朋友可以參考下2014-07-07Jquery實(shí)現(xiàn)顯示和隱藏的4種簡(jiǎn)單方式
顯示和隱藏的效果想必大家都有見到過吧,其實(shí)很簡(jiǎn)單,通過jquery便可輕松實(shí)現(xiàn),下面為大家整理了4種方式,大家可以根據(jù)需求自由選擇2013-08-08