温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS定义Radio单选项和Checkbox复选框样式有效代码怎么写

发布时间:2021-10-08 11:11:08 来源:亿速云 阅读:105 作者:柒染 栏目:web开发

本篇文章为大家展示了CSS定义Radio单选项和Checkbox复选框样式有效代码怎么写,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

我们都知道一般情况利用css来定义Radio单选项和Checkbox复选框样式是无效的,下面我来给大家介绍利用CSS定义Radio单选项和Checkbox复选框样式,有需要了解的朋友可参考。

完全使用css来实例

代码如下:


<style type=”text/css”>
form#form1 {font:12px tahoma,sans-serif}
input[type="checkbox"] {visibility:hidden;width:0;height:0;margin:0;padding:0;}
input[type="checkbox"]+label {background:url(checkstyle.gif) no-repeat;padding-left:18px;color:#ccc;}
input[type="checkbox"]+label:hover {color:#369;background-position:0 -16px;}
input[type="checkbox"]:checked+label {color:#000;background-position:0 -48px;}
input[type="checkbox"]:focus+label {color:#963;background-position:0 -32px;}
input[type="radio"] {visibility:hidden;width:0;height:0;margin:0;padding:0;}
input[type="radio"]+label {background:url(checkstyle.gif) no-repeat 0 -64px;padding-left:18px;color:#ccc;}
input[type="radio"]+label:hover {color:#369;background-position:0 -80px;}
input[type="radio"]:checked+label {color:#000;background-position:0 -112px;}
input[type="radio"]:focus+label {color:#963;background-position:0 -96px;}
</style>


利用js来实例

Html代码

代码如下:


<!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” ”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8&Prime;>
<link rel=”stylesheet” href=”css/screensmall.css” type=”text/css” media=”screen”>
</head>
<body bgcolor=#000000>
<div class=&rsquo;section demo&rsquo;>
<form action=” onsubmit=&rsquo;void(0)&rsquo;>
<h4 style=”color:#ffffff”>Checkboxes</h4>
<div>
<label><input type=”checkbox” checked=”checked”> I&rsquo;m a fancy cross-browser styled checkbox</label>
<div class=&rsquo;leftcol&rsquo;>
<label><input type=”checkbox”> This is a checkbox</label>
<label><input type=”checkbox” checked=”checked”> This is a checkbox</label>
<label><input type=”checkbox”> This is a checkbox</label>
</div>
<div class=&rsquo;rightcol&rsquo;>
<label><input type=”checkbox” checked=”checked”> This is a checkbox</label>
<label><input type=”checkbox”> This is a checkbox</label>
<label><input type=”checkbox” checked=”checked”> This is a checkbox</label>
</div>
<label><input type=”checkbox” checked=”checked”> Apply any CSS styles for different states</label>
</div>
<h4 style=”color:#ffffff”>Radio buttons</h4>
<div class=&rsquo;leftcol&rsquo;>
<label><input type=”radio” name=&rsquo;leftcol&rsquo;> This is a radio button</label>
<label><input type=”radio” checked=”checked” name=&rsquo;leftcol&rsquo;> This is a radio button</label>
<label><input type=”radio” name=&rsquo;leftcol&rsquo;> This is a radio button</label>
</div>
<div class=&rsquo;rightcol&rsquo;>
<label><input type=”radio” name=&rsquo;rightcol&rsquo;> This is a radio button</label>
<label><input type=”radio” checked=”checked” name=&rsquo;rightcol&rsquo;> This is a radio button</label>
<label><input type=”radio” name=&rsquo;rightcol&rsquo;> This is a radio button</label>
</div>
<input type=”reset” value=”Reset Form” style=&rsquo;margin:1em;height:2.5em;background:#222;float:right;color:#fff&rsquo;>
</form>
</div>
<script type=”text/javascript” src=”js/mootools.js”></script>
<script type=”text/javascript” src=”js/moocheck.js”></script>
</body>
</html>
Js代码
var FancyForm = {
start: function(elements, options){
FancyForm.initing = 1;
if($type(elements)!=&rsquo;array&rsquo;) elements = $$(&lsquo;input&rsquo;);
if(!options) options = [];
FancyForm.onclasses = ($type(options['onClasses']) == &rsquo;object&rsquo;) ? options['onClasses'] : {
checkbox: &rsquo;checked&rsquo;,
radio: &rsquo;selected&rsquo;
}
FancyForm.offclasses = ($type(options['offClasses']) == &rsquo;object&rsquo;) ? options['offClasses'] : {
checkbox: &rsquo;unchecked&rsquo;,
radio: &rsquo;unselected&rsquo;
}
if($type(options['extraClasses']) == &rsquo;object&rsquo;){
FancyForm.extra = options['extraClasses'];
} else if(options['extraClasses']){
FancyForm.extra = {
checkbox: &rsquo;f_checkbox&rsquo;,
radio: &rsquo;f_radio&rsquo;,
on: &rsquo;f_on&rsquo;,
off: &rsquo;f_off&rsquo;,
all: &rsquo;fancy&rsquo;
}
} else {
FancyForm.extra = {};
}
FancyForm.onSelect = $pick(options['onSelect'], function(el){});
FancyForm.onDeselect = $pick(options['onDeselect'], function(el){});
var keeps = [];
FancyForm.chks = elements.filter(function(chk){
if( $type(chk) != &rsquo;element&rsquo; ) return false;
if( chk.get(&lsquo;tag&rsquo;) == &rsquo;input&rsquo; && (FancyForm.onclasses[chk.getProperty('type')]) ){
var el = chk.getParent();
if(el.getElement(&lsquo;input&rsquo;)==chk){
el.type = chk.getProperty(&lsquo;type&rsquo;);
el.inputElement = chk;
this.push(el);
} else {
chk.addEvent(&lsquo;click&rsquo;,function(f){
if(f.event.stopPropagation) f.event.stopPropagation();
});
}
} else if( (chk.inputElement = chk.getElement(&lsquo;input&rsquo;)) && (FancyForm.onclasses[(chk.type = chk.inputElement.getProperty('type'))]) ){
return true;
}
return false;
}.bind(keeps));
FancyForm.chks = FancyForm.chks.combine(keeps);
keeps = null;
FancyForm.chks.each(function(chk){
var c = chk.inputElement;
c.setStyle(&lsquo;position&rsquo;, &rsquo;absolute&rsquo;);
c.setStyle(&lsquo;left&rsquo;, &rsquo;-9999px&rsquo;);
chk.addEvent(&lsquo;selectStart&rsquo;, function(f){f.stop()});
chk.name = c.getProperty(&lsquo;name&rsquo;);
FancyForm.update(chk);
});
FancyForm.chks.each(function(chk){
var c = chk.inputElement;
chk.addEvent(&lsquo;click&rsquo;, function(f){
f.stop(); f.type = &rsquo;prop&rsquo;;
c.fireEvent(&lsquo;click&rsquo;, f, 1);
});
chk.addEvent(&lsquo;mousedown&rsquo;, function(f){
if($type(c.onmousedown) == &rsquo;function&rsquo;)
c.onmousedown();
f.preventDefault();
});
chk.addEvent(&lsquo;mouseup&rsquo;, function(f){
if($type(c.onmouseup) == &rsquo;function&rsquo;)
c.onmouseup();
});
c.addEvent(&lsquo;focus&rsquo;, function(f){
if(FancyForm.focus)
chk.setStyle(&lsquo;outline&rsquo;, &rsquo;1px dotted&rsquo;);
});
c.addEvent(&lsquo;blur&rsquo;, function(f){
chk.setStyle(&lsquo;outline&rsquo;, 0);
});
c.addEvent(&lsquo;click&rsquo;, function(f){
if(f.event.stopPropagation) f.event.stopPropagation();
if(c.getProperty(&lsquo;disabled&rsquo;)) // c.getStyle(&lsquo;position&rsquo;) != &rsquo;absolute&rsquo;
return;
if (!chk.hasClass(FancyForm.onclasses[chk.type]))
c.setProperty(&lsquo;checked&rsquo;, &rsquo;checked&rsquo;);
else if(chk.type != &rsquo;radio&rsquo;)
c.setProperty(&lsquo;checked&rsquo;, false);
if(f.type == &rsquo;prop&rsquo;)
FancyForm.focus = 0;
FancyForm.update(chk);
FancyForm.focus = 1;
if(f.type == &rsquo;prop&rsquo; && !FancyForm.initing && $type(c.onclick) == &rsquo;function&rsquo;)
c.onclick();
});
c.addEvent(&lsquo;mouseup&rsquo;, function(f){
if(f.event.stopPropagation) f.event.stopPropagation();
});
c.addEvent(&lsquo;mousedown&rsquo;, function(f){
if(f.event.stopPropagation) f.event.stopPropagation();
});
if(extraclass = FancyForm.extra[chk.type])
chk.addClass(extraclass);
if(extraclass = FancyForm.extra['all'])
chk.addClass(extraclass);
});
FancyForm.initing = 0;
$each($$(&lsquo;form&rsquo;), function(x) {
x.addEvent(&lsquo;reset&rsquo;, function(a) {
window.setTimeout(function(){FancyForm.chks.each(function(x){FancyForm.update(x);x.inputElement.blur()})}, 200);
});
});
},
update: function(chk){
if(chk.inputElement.getProperty(&lsquo;checked&rsquo;)) {
chk.removeClass(FancyForm.offclasses[chk.type]);
chk.addClass(FancyForm.onclasses[chk.type]);
if (chk.type == &rsquo;radio&rsquo;){
FancyForm.chks.each(function(other){
if (other.name == chk.name && other != chk) {
other.inputElement.setProperty(&lsquo;checked&rsquo;, false);
FancyForm.update(other);
}
});
}
if(extraclass = FancyForm.extra['on'])
chk.addClass(extraclass);
if(extraclass = FancyForm.extra['off'])
chk.removeClass(extraclass);
if(!FancyForm.initing)
FancyForm.onSelect(chk);
} else {
chk.removeClass(FancyForm.onclasses[chk.type]);
chk.addClass(FancyForm.offclasses[chk.type]);
if(extraclass = FancyForm.extra['off'])
chk.addClass(extraclass);
if(extraclass = FancyForm.extra['on'])
chk.removeClass(extraclass);
if(!FancyForm.initing)
FancyForm.onDeselect(chk);
}
if(!FancyForm.initing)
chk.inputElement.focus();
},
all: function(){
FancyForm.chks.each(function(chk){
chk.inputElement.setProperty(&lsquo;checked&rsquo;, &rsquo;checked&rsquo;);
FancyForm.update(chk);
});
},
none: function(){
FancyForm.chks.each(function(chk){
chk.inputElement.setProperty(&lsquo;checked&rsquo;, false);
FancyForm.update(chk);
});
}
};
window.addEvent(&lsquo;domready&rsquo;, function(){
FancyForm.start();
});

上述内容就是CSS定义Radio单选项和Checkbox复选框样式有效代码怎么写,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI