建站学习网,专业提供各类建站教程,网页素材,SEO资讯等建站资源!

网站模板_网站源码_网站素材_建站教程_建站学习网

当前位置:建站学习网 > DIV+CSS教程 > DIV CSS教程 >

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

更新时间:2017-01-02整理编辑:建站学习网阅读:0

 我们都知道一般情况利用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″>
<link 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><input type=“checkbox“ checked=“checked“> I’m a fancy cross-browser styled checkbox</label>
<div class=’leftcol’>
<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=’rightcol’>
<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>
<h3 style=“color:#ffffff“>Radio buttons</h3>
<div class=’leftcol’>
<label><input type=“radio“ name=’leftcol’> This is a radio button</label>
<label><input type=“radio“ checked=“checked“ name=’leftcol’> This is a radio button</label>
<label><input type=“radio“ name=’leftcol’> This is a radio button</label>
</div>
<div class=’rightcol’>
<label><input type=“radio“ name=’rightcol’> This is a radio button</label>
<label><input type=“radio“ checked=“checked“ name=’rightcol’> This is a radio button</label>
<label><input type=“radio“ name=’rightcol’> This is a radio button</label>
</div>
<input type=“reset“ value=“Reset Form“ style=’margin: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.initing = 1;
if($type(elements)!=’array’) elements = $$(‘input’);
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’) == ’input’ && (FancyForm.onclasses[chk.getProperty('type')]) ){
var el = chk.getParent();
if(el.getElement(‘input’)==chk){
el.type = chk.getProperty(‘type’);
el.inputElement = chk;
this.push(el);
} else {
chk.addEvent(‘click’,function(f){
if(f.event.stopPropagation) f.event.stopPropagation();
});
}
} else if( (chk.inputElement = chk.getElement(‘input’)) && (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(‘position’, ’absolute’);
c.setStyle(‘left’, ’-9999px’);
chk.addEvent(‘selectStart’, function(f){f.stop()});
chk.name = c.getProperty(‘name’);
FancyForm.update(chk);
});
FancyForm.chks.each(function(chk){
var c = chk.inputElement;
chk.addEvent(‘click’, function(f){
f.stop(); 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(‘outline’, ’1px dotted’);
});
c.addEvent(‘blur’, function(f){
chk.setStyle(‘outline’, 0);
});
c.addEvent(‘click’, function(f){
if(f.event.stopPropagation) f.event.stopPropagation();
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.initing && $type(c.onclick) == ’function’)
c.onclick();
});
c.addEvent(‘mouseup’, function(f){
if(f.event.stopPropagation) f.event.stopPropagation();
});
c.addEvent(‘mousedown’, 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($$(‘form’), function(x) {
x.addEvent(‘reset’, function(a) {
window.setTimeout(function(){FancyForm.chks.each(function(x){FancyForm.update(x);x.inputElement.blur()})}, 200);
});
});
},
update: function(chk){
if(chk.inputElement.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.inputElement.setProperty(‘checked’, 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(‘checked’, ’checked’);
FancyForm.update(chk);
});
},
none: function(){
FancyForm.chks.each(function(chk){
chk.inputElement.setProperty(‘checked’, false);
FancyForm.update(chk);
});
}
};
window.addEvent(‘domready’, function(){
FancyForm.start();
}); 

本文网址:https://www.dedexuexi.com/divcss/jc/1901.html

本站部分文章搜集与网络,如有侵权请联系本站,转载请说明出处。

标签:技巧
收藏此文 赞一下!() 打赏本站

如本文对您有帮助,就请建站学习网抽根烟吧!

支付宝打赏
微信打赏
div+css相对定位和绝对定位用法实例详解
« 上一篇2017年01月02日
div使用margin:0px auto不居中的原因分析及解决
2017年01月02日下一篇 »
  • DIV CSS如何给文字字体添加下划线?
    0阅读
    在css中可以使用text-decoration属性或border-bottom属性来给文字添加下划线样式。下面本篇文章就来给大家介绍一下,希望对大家有所帮助。方法1:使用text-decoration属性设置css的text-decoration属性用于指定添加到文本的修饰,其underline属...
  • 纯CSS3实现带动画效果导航菜单无需js
    0阅读
    随着互联网的发展,网页能表现的东西越来越多。由最开始单纯的文字和链接构成的网页,到后来的表格布局,再到div+css模式,现在发展到了 html+css3。网页能表达的东西越来越多,css3兴起已经
  • CSS定义超链接四个状态的正确顺序L-V-H-A
    0阅读
    css定义超链接是要有先后顺序的。否则,在某些浏览器里面有可能会出现某个样式不起作用的bug。不能正确显示想要的效果。 CSS属性的排列顺序: L-V-H-A 。 L-V-H-A是link、visited、hover、active的简写
  • 在ie7下css居中样式text-align:center;偏左问题解决方法
    0阅读
    css样式text-align:center;在ie7下偏左问题,想必有很多朋友的遇到过吧,下面有个不错的方法,大家可以参考下,希望对大家有所帮助 复制代码 代码如下: body { text-align:center; } 用ie7打开是居左的。
  • 对div盒子模型使用心得总结
    0阅读
    相信每一个从事web开发的人对盒子模型都有一个特殊的理解吧,本文也有一个理解并附有示例代码,喜欢的朋友可以参考下 盒子模型的计算 外边距(margin)+边框(border)+内边距(padding)+内容(content
 
QQ在线咨询