基於jquery擴充漂亮的下拉框可以二次修改

來源:互聯網
上載者:User

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

分解自訂下拉框:

1.建立建構函式,初始化賦值控制項值。

2.繫結控制項呈現在前台。

3.點擊下拉框控制項,展示下拉式清單

4.點擊觸發下拉框控制項,收合下拉式清單。

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

代碼如下:

Html代碼:
複製代碼 代碼如下:
<b class="select_type"></b>

css樣式:
複製代碼 代碼如下:
.dropdown span a{float:left;background:url(/img/Icon_BG.png);}
/*下拉框 http://power.76741.com*/
.dropdown span a{background-position: -213px -75px;}
.dropdown{float:left;width:105px;}
.dropdown span{border:solid 1px #ccc;width:95%;height:28px;background:url(/img/tbline_bg.png);border-radius:8px;overflow:hidden;}
.dropdown span{float:left;padding-left:10px;line-height:28px; cursor:pointer;}
.dropdown span.active{border-radius:8px 8px 0px 0px;}
.dropdown span font{width:auto;margin-right: 0px;float:left;}
.dropdown span a{float:right;width:20px;height:20px;margin:4px 0;}
.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;}
.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;}
.dropdown p a:hover{background:url(/img/tbline_bg.png);color:#666;}

Js代碼:

1、自訂類:
複製代碼 代碼如下:
//下拉框
var ComboBox = function () {
this.tag;
this.data_default;
this.data_list;
this.index = 0;

var _this = this;
var _index, _tag, _value;
//初始化
this.init = function () {
_tag = _this.tag;
_index = _this.index;
//設定對象
_this.setDropdown(_this.data_default, _this.data_list);
//賦值綁定事件
if (_tag.find('span font').length > 0) _value = _tag.find('span font').attr('_id');
if (_tag == undefined) { return false; }
_this.showEvent();
_this.selectedIndex(_index);
return true;
}
//設定下拉式清單
this.setDropdown = function (default_data, list) {
var css = _tag.attr('class');
if (default_data == undefined) {
default_data = { id: 'null', name: '' };
}
var _html = '';
if (_tag.find('p').length > 0 && _tag.find('span').length > 0) {
$.each(list, function (i, value) {
_html += '<a _id="' + value.id + '">' + value.name + '</a>';
});
_tag.find('span font').replaceWith('<font _id="' + default_data.id + '">' + default_data.name + '</font>');
_tag.find('p').html(_html);
} else {
_html = '<div class="dropdown ' + css + '">';
_html += '<span><font _id="' + default_data.id + '">' + default_data.name + '</font><a></a></span>';
_html += '<p>';
if (list) {
$.each(list, function (i, value) {
_html += '<a _id="' + value.id + '">' + value.name + '</a>';
});
}
_html += '</p>';
_html += '</div>';
var parent = _tag.parent();
_tag.replaceWith(_html);
_tag = parent.find('.dropdown' + (css.length > 0 ? '.' + css.replace(' ', '.') : ''));
}
}
//下拉事件
this.showEvent = function () {
_tag.find('span').unbind('click').click(function () {
var p = $(this).parent().find('p');
if (p.css('display') == 'block') {
p.css('display', 'none');
$(this).removeClass('active');
} else if (p.html().length > 0) {
p.css('display', 'block');
$(this).addClass('active');
}
});
}
//選中事件
this.selectedIndex = function (index) {
_tag.find('p a').unbind('click').click(function () {
var parent = $(this).parent().parent();
//給下拉框賦值
if ($(this).text().length > 0) {
var font = parent.find('font');
font.text($(this).text());
font.attr("_id", $(this).attr('_id'));
_this.selectedIndexExpand(parent, $(this).index());
parent.find('span').removeClass('active');
}
parent.find('p').css('display', 'none');
});
if (_tag.find('p a').length <= _index) _index = 0;
if (_value && _value != '') {
_index = _tag.find('p a[_id="' + _value + '"]').index();
}
_tag.find('p a:eq(' + _index + ')').click();
}
//選中事件擴充
this.selectedIndexExpand = function (tag, index) { }
}

2、範例程式碼:
複製代碼 代碼如下:
//http://www.naoqiu.com
var array_state = [{ id: -1, name: '狀態' }, { id: 1, name: '未成功' }, { id: 2, name: '成功' }, { id: 3, name: '失敗'}];
//狀態下拉控制項
var select_type = new ComboBox();
select_type.tag = $('.select_type');
select_type.data_default = array_state[0];
select_type.data_list = array_state;
select_type.selectedIndexExpand = function (tag, index) {
//fun_Pager();
}
select_type.init();

3、樣本圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.