好久没来了,更新下插件,

这个原理就是利用的 input[type='hidden']和自定义属性data-value捆绑传值操作的,可是设置默认选项,回调等参数,代码不多,比较简单,吼吼

(function($){    

    //模拟下拉框
$.fn.htmlSelect = function(opt){
if($(this).length==0) return false;
var opts = $.extend({
defaultsItem : 0, //默认选中的select项
saveInput : '<input type= "hidden" />',
showSelect : '.show',
callback : function(){} //选择完毕后触发回调
},opt); function SimulateSelect(o){
this.o = o;
this.defaultsItem = opts['defaultsItem'];
this.saveInput = opts['saveInput'];
this.callback = opts['callback'];
this.showSelect = this.o.find(opts['showSelect']);
this.hideInp = null;
this.selList = this.o.find('ul');
this.curItem = this. defaultsItem; //当前选中 } SimulateSelect.prototype = {
init : function(){
var _this = this;
_this.creatInput().changeValue().toogleSelList().keyboard(); $(document).on("click", function() { //点击文档空白处 下拉框消失
if(_this.o.hasClass("open")){
_this.closeSelect(_this.o);
}
});
return this;
},
creatInput : function(){
var _this = this;
if($.type(_this.saveInput) == "string"){ //判断若参数为jq对象,则直接指向这个input对象,若为字符串,则插入dom
this.o.append(_this.saveInput);
_this.hideInp = this.o.find('input:hidden');
}else{
_this.hideInp = _this.saveInput
}
if($.type(_this.defaultsItem) == 'number'){
_this.curItem = _this.selList.find('li').eq(_this.defaultsItem);
} return this;
}, changeValue : function(){
var _this = this; _this.curItem.addClass('selected').siblings().removeClass(); var v = _this.curItem.attr('date-value'),s = _this.curItem.html(); _this.showSelect.html(s);
_this.hideInp.val(v); return this;
}, toogleSelList : function(){ //展开收起列表
var _this = this; this.o.on('click',function(e){
if($(this).hasClass("open")){ _this.closeSelect($(this));
}else{ $(this).addClass('open');
_this.selList.show();
} var src = e.target,s = ''; if(src.tagName.toLowerCase() == "li"){
s = src.innerHTML;
_this.showSelect.html(s);
$(src).addClass('selected').siblings().removeClass();
var v = $(src).attr("date-value");
_this.hideInp.val(v);
_this.curItem = $(src); if(_this.callback){
_this.callback(v);
}
} e.stopPropagation();
}) return this;
}, closeSelect : function(obj){
this.selList.hide();
obj.removeClass('open'); return this;
}, keyboard : function(){ //注册键盘事件
var _this = this;
$('body').on('keydown',function(e){ //这块要用body,不然不兼容ie7,8
switch(e.keyCode) {
case 38:
_this.prevItem();
break; case 40:
_this.nextItem();
break;
defaults:
return;
}
})
},
prevItem :function(){
var _this = this;
if(_this.o.hasClass('open')){
if(_this.curItem.prev().length > 0){
_this.curItem = _this.curItem.prev();
_this.changeValue();
}
}
return this; }, nextItem :function(){
var _this = this;
if(_this.o.hasClass('open')){
if(_this.curItem.next().length > 0){
_this.curItem = _this.curItem.next();
_this.changeValue();
} }
return this; } } return this.each(function () {
var $this = $(this);
var obj = new SimulateSelect($this);
obj.init()
}); }
})(jQuery)

css

.select{ position: relative; padding-right: 20px; }
.select .ico2{position: absolute; display: block; right: 0; top:10px;cursor: pointer;}
.select .show{color:#333333; height: 40px; line-height: 40px; min-width: 80px;*margin-top:-10px; text-align: center; font-size: 14px; display: block;padding-left: 10px; cursor: pointer;}
.dropList{ max-height: 510px; overflow: auto; position: absolute; top:40px; width: 100%; left: -1px; background: #FFF; z-index: 99; padding-top: 5px; padding-bottom: 2px; display: none;}
.dropList li{ line-height: 30px;margin:0 3px; padding-left: 10px; cursor: pointer;}
.dropList li:hover{color:#333;}
.dropList li.selected{background: #b9b9b9; color:#FFF;}

html:

<div class="select ">
<span class='show'>中国(+86)</span> <ul class="dropList bd">
<li class='selected' date-value = '0'>中国(+86)</li>
<li date-value = '11'>美国(+1)</li>
<li date-value = '3'>澳大利亚(+61)</li>
<li date-value = '4'>台湾(+668)</li>
<li date-value = '5'>美国(+1))</li>
</ul> </div>

  

调用

$(function(){

    $('#phone').htmlSelect({
callback:function(i){ } //i为当前选中的 data-value的值 })
})

  

 

  

jq插件又来了,模拟select下拉框,支持上下方向键哦的更多相关文章

  1. jquery实现模拟select下拉框效果

    <IGNORE_JS_OP style="WORD-WRAP: break-word"> <!DOCTYPE html PUBLIC "-//W3C// ...

  2. 用div,ul,input模拟select下拉框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. ul -- li 模拟select下拉框

    在写项目中 用到下拉框,一般用 <select name="" id=""> <option value=</option> &l ...

  4. 模拟select下拉框、复选框效果

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  5. [原创]HTML 用div模拟select下拉框

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML xmlns=" ...

  6. div+css模拟select下拉框

    <!DOCTYPE html><html ><head lang="zh"> <meta http-equiv="Content ...

  7. jQuery插件:模拟select下拉菜单

    没搞那么复杂,工作中,基本够用.. <!doctype html> <html> <head> <meta charset="utf-8" ...

  8. css配合js模拟的select下拉框

    css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...

  9. jQuery插件实现select下拉框左右选择_交换内容(multiselect2side)

    效果图: 使用jQuery插件---multiselect2side做法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...

随机推荐

  1. codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  2. shell中的expr命令

    expr 可以进行的操作如下: 逻辑操作 arg1 | arg2 逻辑或操作,真则返回arg1,否则返回arg2(以null或者0来判断参数的真假,有短路功能) arg1 & arg2 逻辑与 ...

  3. sql中NULL的问题

    sql中NULL的问题   今天一不小心在sql中写出以下脚本 select defaultPositionId from TableName where UserId=1100528 and def ...

  4. [C++程序设计]有默认参数的函数

    实参与形参的结合是从左至右顺序进行的.因此指定默认值的参数必须放在形参表列中的最右端,否 则出错.例如: void f1(float a,int b=0,int c,char d=′a′); //不正 ...

  5. POJ2485 最小生成树

    问题:POJ2485 本题求解生成树最大边的最小值 分析: 首先证明生成树最大边的最小值即最小生成树的最大边. 假设:生成树最大边的最小值比最小生成树的最大边更小. 不妨设C为G的一个最小生成树,e是 ...

  6. 世纪大争论:Linux还是GNU/Linux?

    我们在网上已经习惯用“Linux”来称呼Linux操作系统了,然而,偶尔也用“GNU/Linux”来称呼和指代同样的操作系统和软件.同时人们也在争论这两种称呼哪个更合适. 本文将不会选边站队,仅力图向 ...

  7. LINUX SSH客户端的中文乱码问题

       原因在于文件/etc/sysconfig/i18n 这个文件是系统的区域语言设置, i18n是 国际化internationalization的缩写 i和n之间正好18个字母 解释: LANG= ...

  8. NFC应用(三)点对点(P2P)通信

    点对点(P2P)模式允许两个NFC设备之间建立通信链接并交换数据,与读写器.卡模式不一样的就是,P2P模式下数据交互是双向的. P2P遵循ISO18092规范,建立链接后使用NDEF(NFC Data ...

  9. Windows XP SP3中远程桌面实现多用户登陆

    Windows XP SP3配置为支持多用户远程桌面连接,注意:此多用户远程桌面连接必须是不同的用户登录,不能像Windows server 2003那样,同一个用户可以同时登录,只能登陆2个不同用户 ...

  10. UESTC_树上的距离 2015 UESTC Training for Graph Theory<Problem E>

    E - 树上的距离 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Subm ...