radio(单选框)/checkbox(复选框) 美化
由于某种原因,可能需要对单选框(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){//判断是否是ie7\ie8
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; width: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; width: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属性。
radio(单选框)/checkbox(复选框) 美化的更多相关文章
- [oldboy-django][2深入django]Form组件实现生成: select下拉框, checkbox复选框,radio单选框以及如何实现自定义数据格式要求
1 需求 - 1Form组件如何实现生成选择类标签: select,check, radio - 默认值 - 保留上次输入的值 - 2自定义验证规则 - RegexField - -
- 纯css3简单实用的checkbox复选框和radio单选框
昨天为大家分享了一款很炫的checkbox复选框和radio单选框,今天再给大家带来一款简单实用的checkbox复选框和radio单选框.界面清淅.舒服.先给大家来张效果图: 在线预览 源码下载 ...
- 纯css3实现的超炫checkbox复选框和radio单选框
之前为大家分享了好多css3实现的按钮.今天要为大家分享的是纯css3实现的checkbox复选框和radio单选框,效果超级炫.先让我们看看图吧! 在线预览 源码下载 这个实例完全由css3实现 ...
- 2.12 单选框和复选框(radiobox、checkbox)
2.12 单选框和复选框(radiobox.checkbox) 本篇主要介绍单选框和复选框的操作一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是 ...
- Selenium2学习(十五)-- 单选框和复选框(radiobox、checkbox)
本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...
- 如何让checkbox复选框只能单选
function框架div 如何让checkbox复选框只能单选 在项目开发中遇到一个这样的问题,要让一列复选框架在任何时间段内只能选择一个. 有人说怎么不用单选框了,因为单选框一旦选择了就不能取消选 ...
- css input checkbox复选框控件 样式美化的多种方案
checkbox复选框可能是网站中常用的html元素,但大多数人并不满意它的默认样式,这篇文章就讲讲如何实现input checkbox复选框控件 样式美化效果. 资源网站大全 https://55w ...
- Selenium2+python自动化19-单选框和复选框(radiobox、checkbox)
本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...
- JQuery对单选框,复选框,下拉菜单的操作
JSP <%@ page language="java" import="java.util.*" pageEncoding="utf-8&qu ...
随机推荐
- yum安装telnet如何开启telnet服务
1.# yum install -y telnet telnet-server xinetd (其中telnet-server是由xinetd管理的,所以得下载xinetd服务) 2.修 ...
- ElasticSearch - activemq - tomcat 开机自启动
[root@qwy ~]# cat /etc/init.d/elastisearch|grep -v '^#' export JAVA_HOME=/usr/local/java/jdk1..0_172 ...
- 上课笔记:awk
awk [单独的编程语言解释器]1.awk介绍 全称:Aho Weinberger Kernaighan 三个人的首字母缩写: 1970年第一次出现在Unix机器上,后来在开源领域使用它: 所以,我 ...
- MSF魔鬼训练营-5.3 MS08-067安全漏洞实战
msf > search ms08_067 Matching Modules ================ Name D ...
- Docker 添加容器SSH服务
很多时候我们需要登陆到容器内部操作,此时我们就需要开启容器的SSH支持了,下面的小例子将具体介绍三种分配IP地址的方法,分别是pipworl分配,commit分配,Docker分配等. 基于commi ...
- Iterable<T>接口
https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html public interface Iterable<T> ...
- docker在mac下安装及配置阿里云镜像加速
安装 brew cask install docker 配置 关于阿里云加速地址,下面会有详细说明 阿里云官方镜像加速 官方文档:https://help.aliyun.com/document_de ...
- python:split()函数
描述 Python 内置函数 指定分隔符对字符串进行切片 如果参数 num 有指定值,则仅分隔 num 个子字符串 返回分割后的字符串列表. 语法 str.split(str="" ...
- EJS学习(三)之语法规则中
⚠️实例均结合node,也就是AMD规范版本 ejs中使用render()表示渲染文本 接收三个参数:模版字符串.data.options,返回一个字符串 const ejs = require('e ...
- qt webengineview 加载本地资源方式
一.如果把资源添加到本地资源qrc库里了,请使用 ui->preview->setUrl(QUrl("qrc:/HelloWorld2.html")): 二.如果没有现 ...