分解自定義下拉框:
1.創建構造函數,初始化賦值控件值。
2.綁定控件呈現在前臺。
3.點擊下拉框控件,展示下拉列表
4.點擊觸發下拉框控件,收起下拉列表。
5.點擊下拉項觸發事件。
代碼如下:
Html代碼:
代碼如下:
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 += '' + value.name + '';
});
_tag.find('span font').replaceWith('' + default_data.name + '');
_tag.find('p').html(_html);
} else {
_html = '';
_html += '' + default_data.name + '';
_html += '
';
if (list) {
$.each(list, function (i, value) {
_html += '' + value.name + '';
});
}
_html += '
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com