基于jquery擴展漂亮的下拉框可以二次修改
更新時間:2013年11月19日 15:59:07 作者:
以往我在使用下拉框控件老是為了樣式丑陋而煩惱,現(xiàn)在分享這個控件,喜歡的朋友可以進行二次修改,達到你想要的效果
繼續(xù)發(fā)一篇關(guān)于web前端自定義控件——ComboBox(下拉框),以往我在使用下拉框控件老是為了樣式丑陋而煩惱,現(xiàn)在分享這個控件,希望有用的同仁們可以收藏,或進行二次修改,達到你想要的效果。
分解自定義下拉框:
1.創(chuàng)建構(gòu)造函數(shù),初始化賦值控件值。
2.綁定控件呈現(xiàn)在前臺。
3.點擊下拉框控件,展示下拉列表
4.點擊觸發(fā)下拉框控件,收起下拉列表。
5.點擊下拉項觸發(fā)事件。
代碼如下:
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;
//設(shè)置對象
_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;
}
//設(shè)置下拉列表
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: '狀態(tài)' }, { id: 1, name: '未成功' }, { id: 2, name: '成功' }, { id: 3, name: '失敗'}];
//狀態(tài)下拉控件
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、示例圖:
分解自定義下拉框:
1.創(chuàng)建構(gòu)造函數(shù),初始化賦值控件值。
2.綁定控件呈現(xiàn)在前臺。
3.點擊下拉框控件,展示下拉列表
4.點擊觸發(fā)下拉框控件,收起下拉列表。
5.點擊下拉項觸發(fā)事件。
代碼如下:
Html代碼:
復(fù)制代碼 代碼如下:
<b class="select_type"></b>
css樣式:
復(fù)制代碼 代碼如下:
.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、自定義類:
復(fù)制代碼 代碼如下:
//下拉框
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;
//設(shè)置對象
_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;
}
//設(shè)置下拉列表
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、示例代碼:
復(fù)制代碼 代碼如下:
//http://www.naoqiu.com
var array_state = [{ id: -1, name: '狀態(tài)' }, { id: 1, name: '未成功' }, { id: 2, name: '成功' }, { id: 3, name: '失敗'}];
//狀態(tài)下拉控件
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、示例圖:

您可能感興趣的文章:
- jquery multiSelect 多選下拉框
- 基于jquery的網(wǎng)頁SELECT下拉框美化代碼
- jquery獲得下拉框值的代碼
- 基于jquery的無限級聯(lián)下拉框js插件
- 利用jQuery實現(xiàn)可輸入搜索文字的下拉框
- jquery及原生js獲取select下拉框選中的值示例
- jquery動態(tài)加載select下拉框示例代碼
- JQuery異步加載無限下拉框級聯(lián)功能實現(xiàn)示例
- JQuery打造省市下拉框聯(lián)動效果
- jQuery操作select下拉框的text值和value值的方法
- 基于jquery實現(xiàn)的可編輯下拉框?qū)崿F(xiàn)代碼
- jQuery制作簡潔的多級聯(lián)動Select下拉框
- jQuery扁平化風(fēng)格下拉框美化插件FancySelect使用指南
相關(guān)文章
推薦11款jQuery開發(fā)的復(fù)選框和單選框美化插件
web開發(fā)中所有的輸入控件中復(fù)選框和單選框的樣式是最難去設(shè)計的,因為不同的瀏覽器及其操作系統(tǒng)對于樣式的渲染展現(xiàn)是不一樣的。2011-08-08jQuery中對未來的元素綁定事件用bind、live or on
這篇文章主要介紹了jQuery中對未來的元素綁定事件用bind、live or on,需要的朋友可以參考下2014-04-04jQuery ztree實現(xiàn)動態(tài)樹形多選菜單
這篇文章主要介紹了jQuery ztree實現(xiàn)動態(tài)樹形多選菜單,ztree動態(tài)樹形菜單,初始化加載和延遲加載,感興趣的小伙伴們可以參考一下2016-08-08分享8款優(yōu)秀的 jQuery 加載動畫和進度條插件
加載動畫和進度條在網(wǎng)站和 Web 應(yīng)用中的使用非常流行。雖然網(wǎng)速越來越快,但是我們的網(wǎng)站越來越復(fù)雜,同時用戶對網(wǎng)站的使用體驗的要求也越來越高2012-10-10jQuery 常見操作實現(xiàn)方式和常用函數(shù)方法總結(jié)
一個優(yōu)秀的 JavaScript 框架,一篇 jQuery 常用方法及函數(shù)的文章留存?zhèn)渫?/div> 2011-05-05在jquery boxy中添加百度地圖坐標(biāo)拾取注意流程
這篇文章主要介紹了在jquery boxy中添加百度地圖坐標(biāo)拾取注意流程,需要的朋友可以參考下2014-04-04JQuery獲取各種寬度、高度(format函數(shù))實例
本例除了使用標(biāo)準(zhǔn)的JQuery類庫外,還添加了自定義的函數(shù)來進行字符串的format操作。2013-03-03在jquery中的ajax方法怎樣通過JSONP進行遠程調(diào)用
這一節(jié)主要演示下在JQUERY中的ajax方法怎樣通過JSONP進行遠程調(diào)用,需要的朋友可以參考下2014-04-04最新評論