基于jquery自定義的漂亮單選按鈕RadioButton
更新時(shí)間:2013年11月19日 16:06:16 作者:
今天所要分享的控件是單選按鈕,一個(gè)基于jquery擴(kuò)展漂亮的單選按鈕——RadioButton,喜歡的朋友可以收藏下
繼續(xù)分享web前端自定義控件,今天所要分享的控件是單選按鈕,希望對(duì)你有收獲,有好的建議也希望能留言給我。代碼如下:
Html代碼如下:
<div style="margin:50px;float:left;">
<b class="radio" _txt="單選我"></b>
<b class="radio" _txt="單選你"></b>
<b class="radio" _txt="單選他"></b>
</div>
Css代碼如下:
.radio{float:left;background:url(/img/Icon_BG.png);}
.radio{width:14px;height:14px;background-position:0px -58px;cursor:pointer;font-size:9px;}
.radio.checked{background-position: -15px -58px;}
.radio_txt{float:left;margin:0px 0 0 10px;cursor:pointer;line-height:14px;font-size:12px;}
.radio_txt .radio{margin-right:5px;}
Js部分代碼:
1、自定義單選按鈕類(lèi)
//單選項(xiàng)
var RadioButton = function () {
this.obj;
var _this = this, _obj;
//初始化
this.init = function () {
_obj = _this.obj;
var tem = _obj.length > 1 ? _obj.eq(0) : _obj;
if (tem.attr('class').indexOf('radio') == -1) {
showMessage("控件屬性設(shè)置有誤:部分控件并不是單選項(xiàng)!");
return;
}
//單選事件
var click_fun = function (obj) {
if (obj.parent().attr('class') == 'radio_txt') {
obj.parent().parent().find('.radio_txt .radio').removeClass('checked');
} else
obj.siblings('.radio').removeClass('checked');
obj.addClass('checked');
_this.click_callback();
};
//設(shè)置有文字單選項(xiàng)
if (_obj.attr('_txt') != undefined) {
_obj.each(function (i) {
var radio = _obj.eq(i);
radio.wrapAll('<font class="radio_txt"></font>');
//文本單擊事件
radio.parent().append(radio.attr('_txt')).click(function () { click_fun(radio); });
});
} else//對(duì)象點(diǎn)擊事件
_obj.unbind('click').click(function () { click_fun($(this)); });
}
//點(diǎn)擊回調(diào)事件
this.click_callback = function () { }
}
2、實(shí)例化:
//初始化單選框
var radio = new RadioButton();
radio.obj = $('.radio');
radio.init();
示例圖片:
樣式集合圖:
Html代碼如下:
復(fù)制代碼 代碼如下:
<div style="margin:50px;float:left;">
<b class="radio" _txt="單選我"></b>
<b class="radio" _txt="單選你"></b>
<b class="radio" _txt="單選他"></b>
</div>
Css代碼如下:
復(fù)制代碼 代碼如下:
.radio{float:left;background:url(/img/Icon_BG.png);}
.radio{width:14px;height:14px;background-position:0px -58px;cursor:pointer;font-size:9px;}
.radio.checked{background-position: -15px -58px;}
.radio_txt{float:left;margin:0px 0 0 10px;cursor:pointer;line-height:14px;font-size:12px;}
.radio_txt .radio{margin-right:5px;}
Js部分代碼:
1、自定義單選按鈕類(lèi)
復(fù)制代碼 代碼如下:
//單選項(xiàng)
var RadioButton = function () {
this.obj;
var _this = this, _obj;
//初始化
this.init = function () {
_obj = _this.obj;
var tem = _obj.length > 1 ? _obj.eq(0) : _obj;
if (tem.attr('class').indexOf('radio') == -1) {
showMessage("控件屬性設(shè)置有誤:部分控件并不是單選項(xiàng)!");
return;
}
//單選事件
var click_fun = function (obj) {
if (obj.parent().attr('class') == 'radio_txt') {
obj.parent().parent().find('.radio_txt .radio').removeClass('checked');
} else
obj.siblings('.radio').removeClass('checked');
obj.addClass('checked');
_this.click_callback();
};
//設(shè)置有文字單選項(xiàng)
if (_obj.attr('_txt') != undefined) {
_obj.each(function (i) {
var radio = _obj.eq(i);
radio.wrapAll('<font class="radio_txt"></font>');
//文本單擊事件
radio.parent().append(radio.attr('_txt')).click(function () { click_fun(radio); });
});
} else//對(duì)象點(diǎn)擊事件
_obj.unbind('click').click(function () { click_fun($(this)); });
}
//點(diǎn)擊回調(diào)事件
this.click_callback = function () { }
}
2、實(shí)例化:
復(fù)制代碼 代碼如下:
//初始化單選框
var radio = new RadioButton();
radio.obj = $('.radio');
radio.init();
示例圖片:
樣式集合圖:
您可能感興趣的文章:
- jQuery獲取選中單選按鈕radio的值
- JQuery radio(單選按鈕)操作方法匯總
- jquery判斷單選按鈕radio是否選中的方法
- jQuery獲取單選按鈕radio選中值與去除所有radio選中狀態(tài)的方法
- jQuery設(shè)置單選按鈕radio選中/不可用的實(shí)例代碼
- jquery實(shí)現(xiàn)標(biāo)簽支持圖文排列帶上下箭頭按鈕的選項(xiàng)卡
- jQuery模仿單選按鈕選中效果
- jQuery實(shí)現(xiàn)按鈕的點(diǎn)擊 全選/反選 單選框/復(fù)選框 文本框 表單驗(yàn)證
- JQuery操作單選按鈕以及復(fù)選按鈕示例
- jQuery實(shí)現(xiàn)全選按鈕
相關(guān)文章
基于jQuery+JSON的省市二三級(jí)聯(lián)動(dòng)效果
本文給大家分享的是利用jQuery插件,通過(guò)讀取JSON數(shù)據(jù),實(shí)現(xiàn)無(wú)刷新動(dòng)態(tài)下拉省市二(三)級(jí)聯(lián)動(dòng)效果,十分的簡(jiǎn)單實(shí)用,效果也非常棒,有需要的小伙伴可以參考下。2015-06-06
jQuery中元素選擇器(element)簡(jiǎn)單用法示例
這篇文章主要介紹了jQuery中元素選擇器(element)用法,簡(jiǎn)單通俗的說(shuō)明了jQuery元素選擇器的功能、用法并結(jié)合實(shí)例形式分析了jQuery元素選擇器的相關(guān)使用技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05
基于CSS3和jQuery實(shí)現(xiàn)跟隨鼠標(biāo)方位的Hover特效
這篇文章主要介紹了基于CSS3和jQuery實(shí)現(xiàn)跟隨鼠標(biāo)方位的Hover特效的相關(guān)資料,需要的朋友可以參考下2016-07-07
jquery實(shí)現(xiàn)彈出層登錄和全屏層注冊(cè)特效
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)彈出層登錄和全屏層注冊(cè)特效,推薦給大家,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08
jquery 獲取 outerHtml 包含當(dāng)前節(jié)點(diǎn)本身的代碼
jQuery.html() 是獲取當(dāng)前節(jié)點(diǎn)下的html代碼,并不包含當(dāng)前節(jié)點(diǎn)本身的代碼,后來(lái)實(shí)驗(yàn)發(fā)現(xiàn)有一個(gè)jQuery的一個(gè)方法可以解決2014-10-10

