由于某种原因,可能需要对单选框(radio)或复选框(checkbox)进行美化,那么直接修改样式是行不通,要实现就需要添加js,以下js依赖于jquery
radio.js:
function radAchk(dom,cls){ var self = this; if(!cls){ cls = "input-skin"; } dom.each(function(){ var $t = $(this); self.addUI($t,cls); $t.click(function(){ self.eventIE(); }); }); self.eventIE(); } radAchk.prototype.addUI = function(dom,cls){ dom.after($('<span class="'+ cls +'"></span>')); } radAchk.prototype.eventIE = function(dom){ if(window.addEventListener){//判断是否是ie7ie8 return false; } setTimeout(function(){ dom.filter(":checked").trigger('change.checkbox'); dom.filter(":disabled").parent().addClass("disabled"); }); dom.on("change.checkbox",function(){ dom.not(":checked").parent().removeClass("active"); $(this).parent().addClass("active"); }); } ;(function($){ $.fn.radAndchk = function(cls){//cls 皮肤class new radAchk(this,cls); } })(jQuery);
美化单选框(input[type=radio]):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>radio checkbox</title> <style> *{ margin:0; padding:0; } .radio-p{ height:30px; line-height:30px; margin-left:10px; } .js-radio-box{ position:relative; overflow:hidden; padding-left:25px; cursor:pointer; } .js-radio-box input[type=radio]{ opacity:0; filter:alpha(opacity=0); position:absolute; left:0; z-index:2; } .js-radio-box .radio-skin{ position:absolute; left:0; top:50%; margin-top:-7px; display:inline-block; background:url("img/icon.png") no-repeat; 16px; height:16px; z-index:1; } .js-radio-box:hover .radio-skin{ background-position:0 -20px; } .js-radio-box input:disabled + .radio-skin,.js-radio-box.disabled .radio-skin{ background-position:0 -20px; cursor:default; } .js-radio-box input:checked + .radio-skin,.js-radio-box.active .radio-skin{ background-position:0 -40px; } </style> <script src="js/jquery-1.8.2.min.js"></script> </head> <body> <p class="radio-p"></p> <p class="radio-p"><label class="js-radio-box"><input type="radio" checked="true" value="1" name="name1" class="radio1" />单选1</label></p> <p class="radio-p"><label class="js-radio-box"><input type="radio" value="2" name="name1" />单选2</label></p> <p class="radio-p"><label class="js-radio-box"><input type="radio" disabled="disabled" value="3" name="name1" />单选3</label></p> <p class="radio-p"></p> <p class="radio-p"><label class="js-radio-box"><input type="radio" value="12" name="name2" />单选12</label></p> <p class="radio-p"><label class="js-radio-box"><input type="radio" disabled="true" value="13" name="name2" />单选13</label></p> <script src="js/radio.js"></script> <script> $("input[type=radio]").radAndchk("radio-skin");//radio-skin 皮肤class </script> </body> </html>
运行后,样式如下:
美化复选框(input[type=checkbox]):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>checkbox checkbox</title> <style> *{ margin:0; padding:0; } .checkbox-p{ height:30px; line-height:30px; margin-left:10px; } .js-checkbox-box{ position:relative; overflow:hidden; padding-left:25px; cursor:pointer; } .js-checkbox-box input[type=checkbox]{ opacity:0; filter:alpha(opacity=0); position:absolute; left:0; z-index:2; } .js-checkbox-box .checkbox-skin{ position:absolute; left:0; top:50%; margin-top:-7px; display:inline-block; background:url("img/icon-chk.png") no-repeat; 20px; height:16px; z-index:1; } .js-checkbox-box:hover .checkbox-skin{ background-position:0 -40px; } .js-checkbox-box input:disabled + .checkbox-skin,.js-checkbox-box.disabled .checkbox-skin{ background-position:0 -40px; cursor:default; } .js-checkbox-box input:checked + .checkbox-skin,.js-checkbox-box.active .checkbox-skin{ background-position:0 -20px; } </style> <script src="js/jquery-1.8.2.min.js"></script> </head> <body> <p class="checkbox-p"></p> <p class="checkbox-p"><label class="js-checkbox-box"><input type="checkbox" checked="true" value="1" name="name1" class="checkbox1" />单选1</label></p> <p class="checkbox-p"><label class="js-checkbox-box"><input type="checkbox" value="2" name="name1" />单选2</label></p> <p class="checkbox-p"><label class="js-checkbox-box"><input type="checkbox" disabled="disabled" value="3" name="name1" />单选3</label></p> <p class="checkbox-p"></p> <p class="checkbox-p"><label class="js-checkbox-box"><input type="checkbox" value="12" name="name2" />单选12</label></p> <p class="checkbox-p"><label class="js-checkbox-box"><input type="checkbox" disabled="true" value="13" name="name2" />单选13</label></p> <script src="js/radio.js"></script> <script> $("input[type=checkbox]").radAndchk("checkbox-skin");//checkbox-skin 皮肤class </script> </body> </html>
运行后,样式如下:
单选框或复选框的样式可以根据设计图修改。
这个js不影响原本的js方法,如要给radio添加click事件,可以直接:
$("input[type=radio]").click(function(){
……
});
也可以根据需要设置checked、disabled属性。