>  기사  >  웹 프론트엔드  >  jquery를 기반으로 사용자 정의된 아름다운 라디오 버튼 RadioButton

jquery를 기반으로 사용자 정의된 아름다운 라디오 버튼 RadioButton

PHP中文网
PHP中文网원래의
2017-06-06 16:54:271719검색

계속해서 웹 프런트엔드 사용자 정의 컨트롤을 공유하겠습니다. 오늘 공유하고 싶은 컨트롤은 여러분에게 도움이 되기를 바랍니다. 좋은 제안이 있으면 메시지를 남겨주세요. 코드는 다음과 같습니다.

HTML 코드는 다음과 같습니다.

코드 복사 코드는 다음과 같습니다.

<p style="margin:50px;float:left;"> 
<b class="radio" _txt="单选我"></b> 
<b class="radio" _txt="单选你"></b> 
<b class="radio" _txt="单选他"></b> 
</p>

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(&#39;class&#39;).indexOf(&#39;radio&#39;) == -1) { 
showMessage("控件属性设置有误:部分控件并不是单选项!"); 
return; 
} 
//单选事件 
var click_fun = function (obj) { 
if (obj.parent().attr(&#39;class&#39;) == &#39;radio_txt&#39;) { 
obj.parent().parent().find(&#39;.radio_txt .radio&#39;).removeClass(&#39;checked&#39;); 
} else 
obj.siblings(&#39;.radio&#39;).removeClass(&#39;checked&#39;); 
obj.addClass(&#39;checked&#39;); 
_this.click_callback(); 
}; 
//设置有文字单选项 
if (_obj.attr(&#39;_txt&#39;) != undefined) { 
_obj.each(function (i) { 
var radio = _obj.eq(i); 
radio.wrapAll(&#39;<font class="radio_txt"></font>&#39;); 
//文本单击事件 
radio.parent().append(radio.attr(&#39;_txt&#39;)).click(function () { click_fun(radio); }); 
}); 
} else//对象点击事件 
_obj.unbind(&#39;click&#39;).click(function () { click_fun($(this)); }); 
} 
//点击回调事件 
this.click_callback = function () { } 
}


2. 인스턴스화:

코드 복사

//初始化单选框 var radio = new RadioButton(); radio.obj = $(&#39;.radio&#39;); radio.init();

샘플 이미지 :
jquery를 기반으로 사용자 정의된 아름다운 라디오 버튼 RadioButton

스타일 컬렉션 사진 :

jquery를 기반으로 사용자 정의된 아름다운 라디오 버튼 RadioButton

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.