美化select的jquery插件

news/2024/5/17 19:40:09

自己写的一个美化select插件,浏览器自带的实在太丑,还不能用css自定义。

插件主要原理是隐藏原生的select控件,支持select上设置change事件。

脚本

 1 /*
 2 * iSelect 
 3 * 自定义select控件
 4 */
 5 (function ($) {
 6     $.fn.iSelect = function (configs) {
 7         var configs = $.extend({}, $.fn.iSelect.defaults, configs || {});
 8         return this.each(function (index, element) {
 9             var myThis = this;
10             var $this = $(this);
11             
12             var elId = $this.attr('name');
13             if (elId == '' || typeof (elId) == 'undefined') {
14                 elId = 's00' + index;
15                 $this.attr('id', elId);
16             }
17             var $wrap = $('#iselect-' + elId);
18             if ($wrap.length <= 0) {
19                 $wrap = $('<span class="iselect" id="iselect-' + elId + '"><div class="old"></div><div class="text"></div><div class="dropdown"><ul></ul></div></span>');
20                 $this.before($wrap);
21                 $this.prependTo($wrap.find('.old'));
22             }
23             var $text = $wrap.find('.text');
24             var $dropdown = $wrap.find('.dropdown');
25             var width = $this.width();
26             var allwidth = configs.width;
27             if (allwidth == 'auto') {
28                 allwidth =  width;
29             }
30             //$wrap.css({width:allwidth});
31             $text.css({width:allwidth});
32             $dropdown.css({width:allwidth + 2});
33             var $list = $dropdown.find('ul');
34             var html = '';
35             var i = 0;
36             var text = '';
37             var value = '';
38             var selected = false;
39             var style = '';
40             for (i = 0; i < myThis.options.length; i++) {
41                 text = myThis.options[i].text;
42                 value = myThis.options[i].value;
43                 selected = myThis.options[i].selected;
44                 if (selected) {
45                     style = ' class="selected"';
46                 } else
47                     style = '';
48                 html += '<li data-value="' + value + '"' + style + '><a href="javascript:;">' + text + '</a></li>';
49             }
50             $list.html(html);
51             if (myThis.options.length>0) {
52                 text = myThis.options[myThis.selectedIndex].text;
53                 $text.html(text);
54             }
55             
56             $dropdown.hide();
57             $text.click(function (event) {
58                 event.stopPropagation();
59                 $dropdown.show();
60             });
61             $(document.body).click(function () {
62                 $dropdown.hide();
63             });
64             var $items = $list.find('li');
65             $items.click(function (event) {
66                 var selectedIndex = $items.index($(this));
67                 myThis.options[selectedIndex].selected = true;
68                 $(myThis).change();
69                 $text.html(myThis.options[myThis.selectedIndex].text);
70                 $dropdown.hide();
71             });
72         });
73     };
74     $.fn.iSelect.defaults = {width:'auto'};
75 })(jQuery);

样式

.iselect { position:relative; display:inline-block; zoom:1; height:24px; line-height:24px; }
    .iselect .old { height:1px; overflow:hidden; }
    .iselect .text { height:24px; text-indent:5px; background:#fff url(images/iselect.png) no-repeat right center; border:solid 1px #d5d5d5; cursor:default; }
    .iselect .text:hover { border-color:#5999d0; background-image:url(images/iselect_hover.png); }
    .iselect .dropdown { position:absolute; left:0; top:27px; z-index:99; width:100%; height:auto; background:#fff;  }
    .iselect .dropdown ul { border:solid 1px #d5d5d5; max-height:360px; overflow:auto; }
    .iselect .dropdown li { cursor:pointer; }
    .iselect .dropdown li a { display:block; padding:0 5px;  }
    .iselect .dropdown li.selected a { background:#5999d0; color:#eee; }
    .iselect .dropdown a:hover { background:#eee; }

调用

$(function(){
    $('select').iSelect();
});

当前支持一个参数 width用来设置宽度,如果是动态更改下拉选项,在select上调用一次iSelect 即可

转载于:https://www.cnblogs.com/sohowang/p/jquery-select.html


http://www.niftyadmin.cn/n/4051103.html

相关文章

SUSE linux 使用LVM安装系统和管理

引出 在我们安装好linux系统后会发现在需要修改磁盘分区的时候会比较困难,系统安装的/目录的文件系统要更改基本不太可能&#xff0c;其他目录如/home目录也比较困难。但是系统安装时要是采用的LVM管理的方式安装的话就会截然不通。 一、首先普及下LVM的基础知识 LVM&#xff0…

第十二节,运算符

1算数运算 运算符 描述 示例 加&#xff0c;两个对象相加 ab输出结果30 - 减&#xff0c;得到负数&#xff0c;或者一个数减去另一个数 a-b输出结果-10 * 乘&#xff0c;两个数相乘或是返回一个被重复若干次的字符串 a*b输出结果200 / 除&#xf…

hiho_1058_combination_lock

题目大意 给定N个字符&#xff0c;范围为A-Z&#xff0c;编号为1-N&#xff0c;对该字符序列进行M个操作&#xff0c;操作有4中类型&#xff1a; &#xff08;1&#xff09;CMD 1 i j X 将[i, j]区间内的字符均变为X &#xff08;2&#xff09;CMD 2 i j K 将[i, j]区间…

团队项目:第二次冲刺站立会议02

团队项目&#xff1a;第二次冲刺站立会议02 一.昨天干了什么搜集各银行业务的相关资料&#xff0c;注意事项。二.今天准备干什么整理关于银行业务的相关资料&#xff0c;列出所要内容。三.遇到的困难整理资料没什么困难。转载于:https://www.cnblogs.com/kongyuhang/p/5538607.…

回收站删除的文件怎么恢复?清空也能恢复数据

回收站删除的文件怎么恢复&#xff1f;在平时使用电脑的过程中&#xff0c;我们都会时不时将无用的文件清理&#xff0c;甚至将回收站再次清空以节省空间&#xff0c;但有时候难免会发生这样的情况&#xff0c;就是删除的文件还需要再次用到&#xff0c;那怎么从清空的回收站中…

u盘提示格式化怎么办?分享数据恢复小妙招

u盘提示格式化怎么办&#xff1f;u盘是我们常用的一款小工具&#xff0c;但随着时间的积累&#xff0c;难免会存在一些小问题&#xff0c;比如u盘提示格式化&#xff0c;这是什么原因造成的呢&#xff1f;里面的数据还有机会恢复吗&#xff1f;别担心&#xff0c;下面就围绕这些…

硬盘坏了数据可以恢复吗?盘点数据恢复技巧

硬盘坏了数据可以恢复吗&#xff1f;硬盘是计算机中一项主要的存储设备&#xff0c;里面存储了很多重要的数据&#xff0c;但在长期的使用之下&#xff0c;硬盘很容易出现损坏的情况&#xff0c;从而导致数据丢失&#xff0c;恢复硬盘的方法自然是有的&#xff0c;但也要根据不…

删除的照片如何恢复?SD卡数据恢复妙招

删除的照片如何恢复&#xff1f; 生活中我们用到相机的机会很多&#xff0c;如果不小心删除了相机里面的照片有什么补救的方法吗&#xff1f;当然是有的&#xff0c;随着科技的进步&#xff0c;数据恢复软件的功能也越来越强大&#xff0c;下面就给大家分享一下相机照片恢复方法…