基於jquery擴展漂亮的下拉框——ComboBox


關於web前端自定義控件——ComboBox(下拉框),以往我在使用下拉框控件老是為了樣式丑陋而煩惱,現在分享這個控件,希望有用的同仁們可以收藏,或進行二次修改,達到你想要的效果。

分解自定義下拉框:

1。創建構造函數,初始化賦值控件值。

2。綁定控件呈現在前台。

3。點擊下拉框控件,展示下拉列表

4。點擊觸發下拉框控件,收起下拉列表。

5。點擊下拉項觸發事件。


代碼如下:

Html代碼:

 

1 <b class="select_type"></b>


css樣式:

 1 .dropdown span a{float:left;background:url(/img/Icon_BG.png);}
 2 /*下拉框 http://power.76741.com*/
 3 .dropdown span a{background-position: -213px -75px;}
 4 .dropdown{float:left;width:105px;}
 5 .dropdown span{border:solid 1px #ccc;width:95%;height:28px;background:url(/img/tbline_bg.png);border-radius:8px;overflow:hidden;}
 6 .dropdown span{float:left;padding-left:10px;line-height:28px; cursor:pointer;}
 7 .dropdown span.active{border-radius:8px 8px 0px 0px;}
 8 .dropdown span font{width:auto;margin-right: 0px;float:left;}
 9 .dropdown span a{float:right;width:20px;height:20px;margin:4px 0;}
10 .dropdown p{border:solid 1px #ccc;border-top:0px;width:103px;display:none;position:absolute;margin-top:28px;background-color:#fff;z-index:3;max-height:280px;overflow-y: auto; overflow-x: hidden;}
11 .dropdown p a{float:left;line-height:28px;height:28px;padding-left:10px;color:#666;font-size:14px;cursor:default;text-align:left;width:100%;overflow:hidden;}
12 .dropdown p a:hover{background:url(/img/tbline_bg.png);color:#666;} 
 
        

Js代碼:

1。自定義類:

 1 //下拉框
 2 var ComboBox = function () {
 3     this.tag;
 4     this.data_default;
 5     this.data_list;
 6     this.index = 0;
 7 
 8     var _this = this;
 9     var _index, _tag, _value;
10     //初始化
11     this.init = function () {
12         _tag = _this.tag;
13         _index = _this.index;
14         //設置對象
15         _this.setDropdown(_this.data_default, _this.data_list);
16         //賦值綁定事件
17         if (_tag.find('span font').length > 0) _value = _tag.find('span font').attr('_id');
18         if (_tag == undefined) { return false; }
19         _this.showEvent();
20         _this.selectedIndex(_index);
21         return true;
22     }
23     //設置下拉列表
24     this.setDropdown = function (default_data, list) {
25         var css = _tag.attr('class');
26         if (default_data == undefined) {
27             default_data = { id: 'null', name: '' };
28         }
29         var _html = '';
30         if (_tag.find('p').length > 0 && _tag.find('span').length > 0) {
31             $.each(list, function (i, value) {
32                 _html += '<a _id="' + value.id + '">' + value.name + '</a>';
33             });
34             _tag.find('span font').replaceWith('<font _id="' + default_data.id + '">' + default_data.name + '</font>');
35             _tag.find('p').html(_html);
36         } else {
37             _html = '<div class="dropdown ' + css + '">';
38             _html += '<span><font _id="' + default_data.id + '">' + default_data.name + '</font><a></a></span>';
39             _html += '<p>';
40             if (list) {
41                 $.each(list, function (i, value) {
42                     _html += '<a _id="' + value.id + '">' + value.name + '</a>';
43                 });
44             }
45             _html += '</p>';
46             _html += '</div>';
47             var parent = _tag.parent();
48             _tag.replaceWith(_html);
49             _tag = parent.find('.dropdown' + (css.length > 0 ? '.' + css.replace(' ', '.') : ''));
50         }
51     }
52     //下拉事件
53     this.showEvent = function () {
54         _tag.find('span').unbind('click').click(function () {
55             var p = $(this).parent().find('p');
56             if (p.css('display') == 'block') {
57                 p.css('display', 'none');
58                 $(this).removeClass('active');
59             } else if (p.html().length > 0) {
60                 p.css('display', 'block');
61                 $(this).addClass('active');
62             }
63         });
64     }
65     //選中事件
66     this.selectedIndex = function (index) {
67         _tag.find('p a').unbind('click').click(function () {
68             var parent = $(this).parent().parent();
69             //給下拉框賦值
70             if ($(this).text().length > 0) {
71                 var font = parent.find('font');
72                 font.text($(this).text());
73                 font.attr("_id", $(this).attr('_id'));
74                 _this.selectedIndexExpand(parent, $(this).index());
75                 parent.find('span').removeClass('active');
76             }
77             parent.find('p').css('display', 'none');
78         });
79         if (_tag.find('p a').length <= _index) _index = 0;
80         if (_value && _value != '') {
81             _index = _tag.find('p a[_id="' + _value + '"]').index();
82         }
83         _tag.find('p a:eq(' + _index + ')').click();
84     }
85     //選中事件擴展
86     this.selectedIndexExpand = function (tag, index) { }
87 }

2。示例代碼:

 1        var array_state = [{ id: -1, name: '狀態' }, { id: 1, name: '未成功' }, { id: 2, name: '成功' }, { id: 3, name: '失敗'}];
 2        //狀態下拉控件
 3         var select_type = new ComboBox();
 4         select_type.tag = $('.select_type');
 5         select_type.data_default = array_state[0];
 6         select_type.data_list = array_state;
 7         select_type.selectedIndexExpand = function (tag, index) {
 8             //fun_Pager();
 9         }
10         select_type.init();

3.示例圖:

4.下載地址:

jQuery拓展的下拉框控件

http://www.tiaoceng.com/assemblydetail_5.html


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM