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

这个原理就是利用的 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. iOS UIWebView 访问https 绕过证书验证的方法

    在文件开始实现  allowsAnyHTTPSCertificateForHost 方法 @implementation NSURLRequest (NSURLRequestWithIgnoreSSL ...

  2. Js之Location对象

    Window对象的location属性引用的是Location对象,它表示该窗口中当前显示的文档的URL,并定义了方法来使窗口载入新的文档.Document对象的location属性也引用到Locat ...

  3. 利用matlab实现以下功能:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

    程序思路: 对n进行分解质因数,应先找到一个最小的质数k,从2开始,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可. (2)如果n不等于k,则应打印出k的 ...

  4. Eclipse Clojure 开发插件

    参考:http://doc.ccw-ide.org/documentation.html#install-as-plugin 安装Eclipse Clojure插件 这里安装的插件是Countercl ...

  5. Asp.net 处理程序(第五篇)

    HttpApplication有19个标准事件,当到达第8个事件PostMapRequestHandler触发的时候,标志着已经获取到了处理请求的处理程序对象,在第11个事件PreRequestHan ...

  6. C# dynamic类型

    dynamic类型是C#4.0中引入的新类型,它允许其操作掠过编译器类型检查,而在运行时处理.dynami类型在绝大多数情况下和object类型相似,不同之处在于编译器对于包含了dynamic的表达式 ...

  7. IE input X 去掉文本框的叉叉和密码输入框的眼睛图标

    从IE 10开始,type="text" 的 input 在用户输入内容后,会自动产生一个小叉叉(X),方便用户点击清除已经输入的文本对于type="password&q ...

  8. 【图解ASP.NET MVC运行机制理解-简易版】

    很多盆友咨询ASP.NET MVC的机制.网上也有好多.但是都是相当深奥.看的云里雾里的.我今天抽空,整理个简易版本.把处理流程走一遍. 当然,这个只是处理请求的一部分环节.百度的面试题“客户端从浏览 ...

  9. mac安装GNU命令行工具

    mac安装GNU命令行工具 2.添加的repo     tap home/dupes        brew install coreutils  binutils  diffutils  ed -- ...

  10. 开源欣赏wordpress之用户新增user-new.php

    require_once( dirname( __FILE__ ) . '/admin.php' ); 引入根文件. if ( is_multisite() ) { if ( ! current_us ...