jq插件又来了,模拟select下拉框,支持上下方向键哦
好久没来了,更新下插件,
这个原理就是利用的 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下拉框,支持上下方向键哦的更多相关文章
- jquery实现模拟select下拉框效果
<IGNORE_JS_OP style="WORD-WRAP: break-word"> <!DOCTYPE html PUBLIC "-//W3C// ...
- 用div,ul,input模拟select下拉框
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- ul -- li 模拟select下拉框
在写项目中 用到下拉框,一般用 <select name="" id=""> <option value=</option> &l ...
- 模拟select下拉框、复选框效果
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- [原创]HTML 用div模拟select下拉框
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML xmlns=" ...
- div+css模拟select下拉框
<!DOCTYPE html><html ><head lang="zh"> <meta http-equiv="Content ...
- jQuery插件:模拟select下拉菜单
没搞那么复杂,工作中,基本够用.. <!doctype html> <html> <head> <meta charset="utf-8" ...
- css配合js模拟的select下拉框
css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...
- jQuery插件实现select下拉框左右选择_交换内容(multiselect2side)
效果图: 使用jQuery插件---multiselect2side做法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...
随机推荐
- WebBrowser中取对应的图片资源
在客户端中使用webbrower控件,控件中已显示网页,对应的图片资源应已下载完,下面从缓存中找到图片资源,两种方法都可 1.GetUrlCacheEntryInfo http://msdn.micr ...
- 使用DatagramSocket与DatagramPacket传输数据
参考传智播客毕向东JAVA视频. 注: DatagramSocket发送的每个包都需要指定地址,而Socket则是在首次创建时指定地址,以后所有数据的发送都通过此socket. A socket is ...
- css3实现手机qq空间菜单按钮
工作之余写的一个类似于QQzone的菜单效果 先上截图: 图一为点击按钮前界面: 图二为点击按钮后的界面 下面上代码: <!--css部分--> <style type=" ...
- python笔记之subprocess模块
python笔记之subprocess模块 [TOC] 从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spaw ...
- GTW likes math(BC 1001)
GTW likes math Accepts: 472 Submissions: 2140 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 1 ...
- Java 正则提取数字串
例如:有一个字符串:"数量最低2000份",将其中的2000数字提取出来. String arg0 = "数量最低2000份"; Pattern p = Pat ...
- javascript之Arguments
一.Arguments.callee //获取当前正在执行的函数,也就是这个函数自身,常用于获取匿名函数自身 语法:arguments.callee var factorial = function ...
- 【在Windows下进行的编程人员,你真的需要学习下Linux】
从业几年,发现好多编程人员,严重依赖UI工具.对一些命令操作,十分的反感.尤其是从事Windows系统中的程序开发的人员.由于微软对开发工具,编程套件的极限优化.开发出更多的 面向UI的开发工具.从V ...
- Working——流程关系状态表
--主表单 select * from ce_administration_procure t where t.id ='HZe992733d668dc6013d671df4760349'; --流程 ...
- Eclipse设置分级折叠显示项目工程路径
1.抛出问题现象 如下图,这种方式看项目中的代码简直痛苦的要死: 项目迭代越多,工程目录越庞大,可读性就越差. 2.设置分级折叠显示项目 第一步:在Package Explorer视图中找到它的缩放菜 ...