改变checkbox或audio按钮的样式

改变checkbox或audio按钮的样式

( 我这里用jquery实现)

部分代码可以省略,参考者可以根据自己需求改一下

html代码

<div class="wrap">
        <form action="">
            方案一
            <label for="cbox_1" flag="false" class="cbox cbox_1"></label>
            <input type="hidden" id="cbox_1" value="false"/>
            <br/>
            <br/>
            方案二
            <label for="cbox_2" flag="false" class="cbox cbox_2"></label>
            <input type="hidden" id="cbox_2" value="false"/>
        </form>
    </div>

css样式

.wrap {
            width: 300px;
            height: 300px;
            margin: 100px auto;
            box-sizing: border-box;
            border: 2px solid #000;
            padding: 60px;
        }
        .cbox {
            margin-left: 30px;
            vertical-align: middle;
            height: 28px;
            width: 28px;
            display: inline-block;
            font-size: 15px;
            font-weight: 700;
            line-height:1;
            background-color: #fff;
            border: 4px solid #0294FE !important;
            box-sizing: border-box;
            color: #fff;
            cursor: pointer;
        }

js代码

$(function(){
            $(".cbox").on("click", function(){
                if($(this).attr("flag")=="false"){
                    $(this).css("background-color","#0294FE");
                    $(this).html("√");
                    $(this).siblings("input").eq(0).val("true");
                    $(this).attr("flag","ture");

                }else {
                    $(this).css("background-color","#fff");
                    $(this).html("");
                    $(this).siblings("input").eq(0).val("false");
                    $(this).attr("flag","false");
                }
                //  判断点击的是哪个按钮
                //  如果点击的是第一个按钮则改变第二个按钮的状态
                //  如果点击的是第二个按钮则改变第一个按钮的状态
                if ($(this).get(0) == $(".cbox_1").get(0)){
                    $(".cbox_2").css("background-color","#fff");
                    $(".cbox_2").html("");
                    $(".cbox_2").siblings("input").eq(0).val("false");
                    $(".cbox_2").attr("flag","false");
                }else {
                    $(".cbox_1").css("background-color","#fff");
                    $(".cbox_1").html("");
                    $(".cbox_1").siblings("input").eq(0).val("false");
                    $(".cbox_1").attr("flag","false");
                }
            });

        })

整体代码如下

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>改变复选框样式</title>
    <style>
        .wrap {
            width: 300px;
            height: 300px;
            margin: 100px auto;
            box-sizing: border-box;
            border: 2px solid #000;
            padding: 60px;
        }
        .cbox {
            margin-left: 30px;
            vertical-align: middle;
            height: 28px;
            width: 28px;
            display: inline-block;
            font-size: 15px;
            font-weight: 700;
            line-height:1;
            background-color: #fff;
            border: 4px solid #0294FE !important;
            box-sizing: border-box;
            color: #fff;
            cursor: pointer;
        }
    </style>
    <script src="./javascript/jquery-3.1.1.min.js"></script>
</head>
<body>
    <div class="wrap">
        <form action="">
            方案一
            <label for="cbox_1" flag="false" class="cbox cbox_1"></label>
            <input type="hidden" id="cbox_1" value="false"/>
            <br/>
            <br/>
            方案二
            <label for="cbox_2" flag="false" class="cbox cbox_2"></label>
            <input type="hidden" id="cbox_2" value="false"/>
        </form>
    </div>
    <script>
        // checkbox 选择按钮改变
        $(function(){
            $(".cbox").on("click", function(){
                if($(this).attr("flag")=="false"){
                    $(this).css("background-color","#0294FE");
                    $(this).html("√");
                    $(this).siblings("input").eq(0).val("true");
                    $(this).attr("flag","ture");

                }else {
                    $(this).css("background-color","#fff");
                    $(this).html("");
                    $(this).siblings("input").eq(0).val("false");
                    $(this).attr("flag","false");
                }
                //  判断点击的是哪个按钮
                //  如果点击的是第一个按钮则改变第二个按钮的状态
                //  如果点击的是第二个按钮则改变第一个按钮的状态
                if ($(this).get(0) == $(".cbox_1").get(0)){
                    $(".cbox_2").css("background-color","#fff");
                    $(".cbox_2").html("");
                    $(".cbox_2").siblings("input").eq(0).val("false");
                    $(".cbox_2").attr("flag","false");
                }else {
                    $(".cbox_1").css("background-color","#fff");
                    $(".cbox_1").html("");
                    $(".cbox_1").siblings("input").eq(0).val("false");
                    $(".cbox_1").attr("flag","false");
                }
            });

        })

    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值