基于jquery自定義的漂亮單選按鈕RadioButton
更新時間:2013年11月19日 16:06:16 作者:
今天所要分享的控件是單選按鈕,一個基于jquery擴(kuò)展漂亮的單選按鈕——RadioButton,喜歡的朋友可以收藏下
繼續(xù)分享web前端自定義控件,今天所要分享的控件是單選按鈕,希望對你有收獲,有好的建議也希望能留言給我。代碼如下:
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、自定義單選按鈕類
//單選項
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è)置有誤:部分控件并不是單選項!");
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è)置有文字單選項
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//對象點(diǎn)擊事件
_obj.unbind('click').click(function () { click_fun($(this)); });
}
//點(diǎn)擊回調(diào)事件
this.click_callback = function () { }
}
2、實例化:
//初始化單選框
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、自定義單選按鈕類
復(fù)制代碼 代碼如下:
//單選項
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è)置有誤:部分控件并不是單選項!");
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è)置有文字單選項
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//對象點(diǎn)擊事件
_obj.unbind('click').click(function () { click_fun($(this)); });
}
//點(diǎn)擊回調(diào)事件
this.click_callback = function () { }
}
2、實例化:
復(fù)制代碼 代碼如下:
//初始化單選框
var radio = new RadioButton();
radio.obj = $('.radio');
radio.init();
示例圖片:
樣式集合圖:
您可能感興趣的文章:
相關(guān)文章
基于CSS3和jQuery實現(xiàn)跟隨鼠標(biāo)方位的Hover特效
這篇文章主要介紹了基于CSS3和jQuery實現(xiàn)跟隨鼠標(biāo)方位的Hover特效的相關(guān)資料,需要的朋友可以參考下2016-07-07
jquery 獲取 outerHtml 包含當(dāng)前節(jié)點(diǎn)本身的代碼
jQuery.html() 是獲取當(dāng)前節(jié)點(diǎn)下的html代碼,并不包含當(dāng)前節(jié)點(diǎn)本身的代碼,后來實驗發(fā)現(xiàn)有一個jQuery的一個方法可以解決2014-10-10

