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();
});

时间: 2024-10-29 16:16:26

CSS定义Radio单选项和Checkbox复选框样式的相关文章

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;

纯CSS设置Checkbox复选框控件的样式的例子

Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至可以让它看起来一点也不像复选框. 在本教程中,我们将创建5个不同的选择框,你可以在你的网站上使用它. 首先,需要添加一段CSS隐藏所有的Checkbox复选框,下面我们会改变它的外观.要做到点需要添加一段代码到你的CSS文件中. /**  * 隐藏默认的checkbox  */ input[type

javascript获取checkbox复选框获取选中的选项_javascript技巧

有关javascript 获取checkbox复选框的实例数不胜数,下面的这个示例,纯js实现的 var form = document.getElementById("form2"); var field = form.elements["test2"]; var option = Dining.getSelectedOption(form, field); var message = ""; for (var i = 0, len = opt

js获取checkbox复选框选中的选项实例_javascript技巧

有关javascript 获取checkbox复选框的实例数不胜数. js实现: var form = document.getElementById("form2"); var field = form.elements["test2"]; var option = Dining.getSelectedOption(form, field); var message = ""; for (var i = 0, len = option.leng

javascript实现checkbox复选框实例代码_javascript技巧

本文实例介绍了javascript实现checkbox复选框实例代码以及对checkbox复选框进行美化操作,分享给大家供大家参考,具体内容如下 1.checkbox复选框进行美化操作 复选框默认外表的美观度差强人意,能够满足美观度要求不高的页面,但是如果对于页面要求较为精致,那可能就过于勉强了,下面就一段对复选框进行美化的代码实例,希望能够给大家带来一定的帮助. 代码实例如下: <!DOCTYPE html> <html> <head> <meta charse

js点击文本框弹出可选择的checkbox复选框_javascript技巧

本文分享一段代码实例,它能够点击文本框的时候,能够弹出下拉的checkbox复选框,选中复选框就能够将值写入文本框中,可能在实际应用中的效果没有这么直白简单,不过可以作为一个例子演示,以便于学习者理解和扩展. 代码如下: <html> <head> <meta charset="gb2312"> <title>js点击文本框弹出可选择的checkbox复选框</title> <style type="text/

javascript动态添加checkbox复选框的方法_javascript技巧

本文实例为大家介绍了javascript如何动态添加checkbox复选框: 在实际应用中可能需要动态的添加复选框,下面就简单介绍一下如何实现此效果. 单纯的创建一个复选框是很容易的,代码如下: var oCheckbox=document.createElement("input"); oCheckbox.setAttribute("type","checkbox"); oCheckbox.setAttribute("id"

Extjs 4.x 得到form CheckBox 复选框的值

  CheckBox(复选框)主要用来接收用户选择的选项,那么如何通过Extjs 4.x 得到form CheckBox的值呢?下面有个不错的方法,大家值得一看 CheckBox(复选框)主要用来接收用户选择的选项 如图所示(请忽略UI的不好看): 该弹出窗口的主要代码如下: 代码如下: var win = new Ext.Window({ modal : true, title : '确定要拒绝该表吗?', width : 500, plain : true, items : [fp] });

ASP.NET中 CheckBox复选框控件的使用_基础应用

我们可以使用两种类型的 ASP.NET 控件将复选框添加到 Web 窗体页上:单独的 CheckBox 控件或 CheckBoxList 控件.两种控件都为用户提供了一种输入布尔型数据(真或假.是或否)的方法. 这里我们单独使用CheckBox,先来看看它的属性 属性 描述 .NET AutoPostBack 规定在 Checked 属性已改变后,是否立即向服务器回传表单.默认是 false. 1.0 CausesValidation 规定点击 Button 控件时是否执行验证. 2.0 Che