zoukankan      html  css  js  c++  java
  • 模拟单选框,多选框

      默认的单选多选框样式不能满足我们的需求,而css又不兼容低版本ie,因此,很多时候,我们会用一些span,div等标签来模拟他们。本次我用了label。原理是给label绑定事件点击切换class来切换选中样式。好处是不用绑定input的选中事件,点击label,input会自行选中。样式如图(样式自由定义,个人可自行发挥)

    html代码如下:

    <span>checkbox</span>
    <label class="ui-checkbox"><input type="checkbox" name="check" value="1"></label>
    <label class="ui-checkbox"><input type="checkbox" name="check" value="2"></label>
    <label class="ui-checkbox"><input type="checkbox" name="check" value="3"></label>
    <span>radio</span>
    <label class="ui-radio"><input type="radio" name="radio" value="1"></label>
    <label class="ui-radio"><input type="radio" name="radio" value="1"></label>
    <label class="ui-radio"><input type="radio" name="radio" value="1"></label>

    css代码:

    input{display:none}
    .ui-checkbox{
    background-color: white;
    border-radius: 5px;
    border:1px solid #d3d3d3;
    width:20px;
    height:20px;
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    line-height: 20px;
    }
    .ui-radio{
    background-color: white;
    border-radius: 15px;
    border:1px solid #d3d3d3;
    width:20px;
    height:20px;
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    line-height: 20px;
    }
    .checked{
    background-color: #eee;
    }
    .checked:after{
    content:"2714";
    }

    js代码:(注:不要忘记引jquery)

    $(document).on('click','.ui-checkbox',function(e){
        if(e.target.tagName.toUpperCase()=='LABEL'){
            $(this).toggleClass('checked');
        }
    });
    $(document).on('click','.ui-radio',function(e){
        if(e.target.tagName.toUpperCase()=='LABEL'){
            var elenName = $(this).find("input").prop("name");
            $('input[name='+elenName+']').parent('label').removeClass('checked');
            $(this).addClass('checked');
        }
    });

    熟悉了远离及思路,可自行修改优化。

  • 相关阅读:
    WCF客户端链接服务超时客户端close
    C# byte数组常用扩展浅析(转)
    代码生成相关工具及技术
    已处理证书链,但是在不受信任提供程序信任的根证书中终止。
    清理SQL Server数据库日志的两种方法
    开源框架项目列表
    SQL Server数据库文件恢复技术
    VS2008找不到导出模板
    jquery 学习笔记(二)
    方法的参数的默认值设置
  • 原文地址:https://www.cnblogs.com/jidi/p/customInput.html
Copyright © 2011-2022 走看看