CSS定义Radio单选项和Checkbox复选框样式有效代码
网站建设 2023-01-28 21:39www.1681989.com免费网站
我们都知道一般情况利用css来定义Radio单选项和Checkbox复选框样式是无效的,狼蚁网站SEO优化我来给大家介绍利用CSS定义Radio单选项和Checkbox复选框样式,有需要了解的朋友可参考。
完全使用css来实例
<style type=”text/css”>
form#form1 {font:12px tahoma,sans-serif}
put[type="checkbox"] {visibility:hidden;width:0;height:0;marg:0;paddg:0;}
put[type="checkbox"]+label {background:url(checkstyle.gif) no-repeat;paddg-left:18px;color:#c;}
put[type="checkbox"]+label:hover {color:#369;background-position:0 -16px;}
put[type="checkbox"]:checked+label {color:#000;background-position:0 -48px;}
put[type="checkbox"]:focus+label {color:#963;background-position:0 -32px;}
put[type="radio"] {visibility:hidden;width:0;height:0;marg:0;paddg:0;}
put[type="radio"]+label {background:url(checkstyle.gif) no-repeat 0 -64px;paddg-left:18px;color:#c;}
put[type="radio"]+label:hover {color:#369;background-position:0 -80px;}
put[type="radio"]:checked+label {color:#000;background-position:0 -112px;}
put[type="radio"]:focus+label {color:#963;background-position:0 -96px;}
</style>
利用js来实例
Html代码
<!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” ”http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://.w3./1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″>
<lk rel=”stylesheet” href=”css/screensmall.css” type=”text/css” media=”screen”>
</head>
<body bgcolor=#000000>
<div class=’section demo’>
<form action=” onsubmit=’void(0)’>
<h3 style=”color:#ffffff”>Checkboxes</h3>
<div>
<label><put type=”checkbox” checked=”checked”> I’m a fancy cross-browser styled checkbox</label>
<div class=’leftcol’>
<label><put type=”checkbox”> This is a checkbox</label>
<label><put type=”checkbox” checked=”checked”> This is a checkbox</label>
<label><put type=”checkbox”> This is a checkbox</label>
</div>
<div class=’rightcol’>
<label><put type=”checkbox” checked=”checked”> This is a checkbox</label>
<label><put type=”checkbox”> This is a checkbox</label>
<label><put type=”checkbox” checked=”checked”> This is a checkbox</label>
</div>
<label><put type=”checkbox” checked=”checked”> Apply any CSS styles for different states</label>
</div>
<h3 style=”color:#ffffff”>Radio buttons</h3>
<div class=’leftcol’>
<label><put type=”radio” name=’leftcol’> This is a radio button</label>
<label><put type=”radio” checked=”checked” name=’leftcol’> This is a radio button</label>
<label><put type=”radio” name=’leftcol’> This is a radio button</label>
</div>
<div class=’rightcol’>
<label><put type=”radio” name=’rightcol’> This is a radio button</label>
<label><put type=”radio” checked=”checked” name=’rightcol’> This is a radio button</label>
<label><put type=”radio” name=’rightcol’> This is a radio button</label>
</div>
<put type=”reset” value=”Reset Form” style=’marg:1em;height:2.5em;background:#222;float:right;color:#fff’>
</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.itg = 1;
if($type(elements)!=’array’) elements = $$(‘put’);
if(!options) options = [];
FancyForm.onclasses = ($type(options['onClasses']) == ’object’) ? options['onClasses'] : {
checkbox: ’checked’,
radio: ’selected’
}
FancyForm.offclasses = ($type(options['offClasses']) == ’object’) ? options['offClasses'] : {
checkbox: ’unchecked’,
radio: ’unselected’
}
if($type(options['extraClasses']) == ’object’){
FancyForm.extra = options['extraClasses'];
} else if(options['extraClasses']){
FancyForm.extra = {
checkbox: ’f_checkbox’,
radio: ’f_radio’,
on: ’f_on’,
off: ’f_off’,
all: ’fancy’
}
} 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) != ’element’ ) return false;
if( chk.get(‘tag’) == ’put’ && (FancyForm.onclasses[chk.getProperty('type')]) ){
var el = chk.getParent();
if(el.getElement(‘put’)==chk){
el.type = chk.getProperty(‘type’);
el.putElement = chk;
this.push(el);
} else {
chk.addEvent(‘click’,function(f){
if(f.event.sPropagation) f.event.sPropagation();
});
}
} else if( (chk.putElement = chk.getElement(‘put’)) && (FancyForm.onclasses[(chk.type = chk.putElement.getProperty('type'))]) ){
return true;
}
return false;
}.bd(keeps));
FancyForm.chks = FancyForm.chks.be(keeps);
keeps = null;
FancyForm.chks.each(function(chk){
var c = chk.putElement;
c.setStyle(‘position’, ’absolute’);
c.setStyle(‘left’, ’-9999px’);
chk.addEvent(‘selectStart’, function(f){f.s()});
chk.name = c.getProperty(‘name’);
FancyForm.update(chk);
});
FancyForm.chks.each(function(chk){
var c = chk.putElement;
chk.addEvent(‘click’, function(f){
f.s(); f.type = ’prop’;
c.fireEvent(‘click’, f, 1);
});
chk.addEvent(‘mousedown’, function(f){
if($type(c.onmousedown) == ’function’)
c.onmousedown();
f.preventDefault();
});
chk.addEvent(‘mouseup’, function(f){
if($type(c.onmouseup) == ’function’)
c.onmouseup();
});
c.addEvent(‘focus’, function(f){
if(FancyForm.focus)
chk.setStyle(‘outle’, ’1px dotted’);
});
c.addEvent(‘blur’, function(f){
chk.setStyle(‘outle’, 0);
});
c.addEvent(‘click’, function(f){
if(f.event.sPropagation) f.event.sPropagation();
if(c.getProperty(‘disabled’)) // c.getStyle(‘position’) != ’absolute’
return;
if (!chk.hasClass(FancyForm.onclasses[chk.type]))
c.setProperty(‘checked’, ’checked’);
else if(chk.type != ’radio’)
c.setProperty(‘checked’, false);
if(f.type == ’prop’)
FancyForm.focus = 0;
FancyForm.update(chk);
FancyForm.focus = 1;
if(f.type == ’prop’ && !FancyForm.itg && $type(c.onclick) == ’function’)
c.onclick();
});
c.addEvent(‘mouseup’, function(f){
if(f.event.sPropagation) f.event.sPropagation();
});
c.addEvent(‘mousedown’, function(f){
if(f.event.sPropagation) f.event.sPropagation();
});
if(extraclass = FancyForm.extra[chk.type])
chk.addClass(extraclass);
if(extraclass = FancyForm.extra['all'])
chk.addClass(extraclass);
});
FancyForm.itg = 0;
$each($$(‘form’), function(x) {
x.addEvent(‘reset’, function(a) {
wdow.setTimeout(function(){FancyForm.chks.each(function(x){FancyForm.update(x);x.putElement.blur()})}, 200);
});
});
},
update: function(chk){
if(chk.putElement.getProperty(‘checked’)) {
chk.removeClass(FancyForm.offclasses[chk.type]);
chk.addClass(FancyForm.onclasses[chk.type]);
if (chk.type == ’radio’){
FancyForm.chks.each(function(other){
if (other.name == chk.name && other != chk) {
other.putElement.setProperty(‘checked’, false);
FancyForm.update(other);
}
});
}
if(extraclass = FancyForm.extra['on'])
chk.addClass(extraclass);
if(extraclass = FancyForm.extra['off'])
chk.removeClass(extraclass);
if(!FancyForm.itg)
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.itg)
FancyForm.onDeselect(chk);
}
if(!FancyForm.itg)
chk.putElement.focus();
},
all: function(){
FancyForm.chks.each(function(chk){
chk.putElement.setProperty(‘checked’, ’checked’);
FancyForm.update(chk);
});
},
none: function(){
FancyForm.chks.each(function(chk){
chk.putElement.setProperty(‘checked’, false);
FancyForm.update(chk);
});
}
};
wdow.addEvent(‘domready’, function(){
FancyForm.start();
});
完全使用css来实例
复制代码
代码如下:<style type=”text/css”>
form#form1 {font:12px tahoma,sans-serif}
put[type="checkbox"] {visibility:hidden;width:0;height:0;marg:0;paddg:0;}
put[type="checkbox"]+label {background:url(checkstyle.gif) no-repeat;paddg-left:18px;color:#c;}
put[type="checkbox"]+label:hover {color:#369;background-position:0 -16px;}
put[type="checkbox"]:checked+label {color:#000;background-position:0 -48px;}
put[type="checkbox"]:focus+label {color:#963;background-position:0 -32px;}
put[type="radio"] {visibility:hidden;width:0;height:0;marg:0;paddg:0;}
put[type="radio"]+label {background:url(checkstyle.gif) no-repeat 0 -64px;paddg-left:18px;color:#c;}
put[type="radio"]+label:hover {color:#369;background-position:0 -80px;}
put[type="radio"]:checked+label {color:#000;background-position:0 -112px;}
put[type="radio"]:focus+label {color:#963;background-position:0 -96px;}
</style>
利用js来实例
Html代码
复制代码
代码如下:<!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” ”http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://.w3./1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″>
<lk rel=”stylesheet” href=”css/screensmall.css” type=”text/css” media=”screen”>
</head>
<body bgcolor=#000000>
<div class=’section demo’>
<form action=” onsubmit=’void(0)’>
<h3 style=”color:#ffffff”>Checkboxes</h3>
<div>
<label><put type=”checkbox” checked=”checked”> I’m a fancy cross-browser styled checkbox</label>
<div class=’leftcol’>
<label><put type=”checkbox”> This is a checkbox</label>
<label><put type=”checkbox” checked=”checked”> This is a checkbox</label>
<label><put type=”checkbox”> This is a checkbox</label>
</div>
<div class=’rightcol’>
<label><put type=”checkbox” checked=”checked”> This is a checkbox</label>
<label><put type=”checkbox”> This is a checkbox</label>
<label><put type=”checkbox” checked=”checked”> This is a checkbox</label>
</div>
<label><put type=”checkbox” checked=”checked”> Apply any CSS styles for different states</label>
</div>
<h3 style=”color:#ffffff”>Radio buttons</h3>
<div class=’leftcol’>
<label><put type=”radio” name=’leftcol’> This is a radio button</label>
<label><put type=”radio” checked=”checked” name=’leftcol’> This is a radio button</label>
<label><put type=”radio” name=’leftcol’> This is a radio button</label>
</div>
<div class=’rightcol’>
<label><put type=”radio” name=’rightcol’> This is a radio button</label>
<label><put type=”radio” checked=”checked” name=’rightcol’> This is a radio button</label>
<label><put type=”radio” name=’rightcol’> This is a radio button</label>
</div>
<put type=”reset” value=”Reset Form” style=’marg:1em;height:2.5em;background:#222;float:right;color:#fff’>
</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.itg = 1;
if($type(elements)!=’array’) elements = $$(‘put’);
if(!options) options = [];
FancyForm.onclasses = ($type(options['onClasses']) == ’object’) ? options['onClasses'] : {
checkbox: ’checked’,
radio: ’selected’
}
FancyForm.offclasses = ($type(options['offClasses']) == ’object’) ? options['offClasses'] : {
checkbox: ’unchecked’,
radio: ’unselected’
}
if($type(options['extraClasses']) == ’object’){
FancyForm.extra = options['extraClasses'];
} else if(options['extraClasses']){
FancyForm.extra = {
checkbox: ’f_checkbox’,
radio: ’f_radio’,
on: ’f_on’,
off: ’f_off’,
all: ’fancy’
}
} 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) != ’element’ ) return false;
if( chk.get(‘tag’) == ’put’ && (FancyForm.onclasses[chk.getProperty('type')]) ){
var el = chk.getParent();
if(el.getElement(‘put’)==chk){
el.type = chk.getProperty(‘type’);
el.putElement = chk;
this.push(el);
} else {
chk.addEvent(‘click’,function(f){
if(f.event.sPropagation) f.event.sPropagation();
});
}
} else if( (chk.putElement = chk.getElement(‘put’)) && (FancyForm.onclasses[(chk.type = chk.putElement.getProperty('type'))]) ){
return true;
}
return false;
}.bd(keeps));
FancyForm.chks = FancyForm.chks.be(keeps);
keeps = null;
FancyForm.chks.each(function(chk){
var c = chk.putElement;
c.setStyle(‘position’, ’absolute’);
c.setStyle(‘left’, ’-9999px’);
chk.addEvent(‘selectStart’, function(f){f.s()});
chk.name = c.getProperty(‘name’);
FancyForm.update(chk);
});
FancyForm.chks.each(function(chk){
var c = chk.putElement;
chk.addEvent(‘click’, function(f){
f.s(); f.type = ’prop’;
c.fireEvent(‘click’, f, 1);
});
chk.addEvent(‘mousedown’, function(f){
if($type(c.onmousedown) == ’function’)
c.onmousedown();
f.preventDefault();
});
chk.addEvent(‘mouseup’, function(f){
if($type(c.onmouseup) == ’function’)
c.onmouseup();
});
c.addEvent(‘focus’, function(f){
if(FancyForm.focus)
chk.setStyle(‘outle’, ’1px dotted’);
});
c.addEvent(‘blur’, function(f){
chk.setStyle(‘outle’, 0);
});
c.addEvent(‘click’, function(f){
if(f.event.sPropagation) f.event.sPropagation();
if(c.getProperty(‘disabled’)) // c.getStyle(‘position’) != ’absolute’
return;
if (!chk.hasClass(FancyForm.onclasses[chk.type]))
c.setProperty(‘checked’, ’checked’);
else if(chk.type != ’radio’)
c.setProperty(‘checked’, false);
if(f.type == ’prop’)
FancyForm.focus = 0;
FancyForm.update(chk);
FancyForm.focus = 1;
if(f.type == ’prop’ && !FancyForm.itg && $type(c.onclick) == ’function’)
c.onclick();
});
c.addEvent(‘mouseup’, function(f){
if(f.event.sPropagation) f.event.sPropagation();
});
c.addEvent(‘mousedown’, function(f){
if(f.event.sPropagation) f.event.sPropagation();
});
if(extraclass = FancyForm.extra[chk.type])
chk.addClass(extraclass);
if(extraclass = FancyForm.extra['all'])
chk.addClass(extraclass);
});
FancyForm.itg = 0;
$each($$(‘form’), function(x) {
x.addEvent(‘reset’, function(a) {
wdow.setTimeout(function(){FancyForm.chks.each(function(x){FancyForm.update(x);x.putElement.blur()})}, 200);
});
});
},
update: function(chk){
if(chk.putElement.getProperty(‘checked’)) {
chk.removeClass(FancyForm.offclasses[chk.type]);
chk.addClass(FancyForm.onclasses[chk.type]);
if (chk.type == ’radio’){
FancyForm.chks.each(function(other){
if (other.name == chk.name && other != chk) {
other.putElement.setProperty(‘checked’, false);
FancyForm.update(other);
}
});
}
if(extraclass = FancyForm.extra['on'])
chk.addClass(extraclass);
if(extraclass = FancyForm.extra['off'])
chk.removeClass(extraclass);
if(!FancyForm.itg)
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.itg)
FancyForm.onDeselect(chk);
}
if(!FancyForm.itg)
chk.putElement.focus();
},
all: function(){
FancyForm.chks.each(function(chk){
chk.putElement.setProperty(‘checked’, ’checked’);
FancyForm.update(chk);
});
},
none: function(){
FancyForm.chks.each(function(chk){
chk.putElement.setProperty(‘checked’, false);
FancyForm.update(chk);
});
}
};
wdow.addEvent(‘domready’, function(){
FancyForm.start();
});