jq实现点击开关

开关是同一个事物控制两种不同的状态,开关是唯一的,但是内容是改变的 例如:开和关的切换,本来为开的状态,点击切换为关的状态,开关随意切换。

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>jquery实现开关按钮</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <style>
        .div1{
            width: 60px;
            height: 26px;
            border-radius: 15px;
            overflow: hidden;
            position: relative;
        }
        .div1 .left{
            position: absolute;
            left:4px;
        }
        .div1 .right{
            position: absolute;
            right:4px;
        }
        .div2{
            width: 30px;
            height: 20px;
            border-radius: 9px;
            background: #fff;
            position: absolute;
        }
        .open1{
            background: #ddd;
        }
        .open2{
            top: 3px;
            left: 5px;
        }
        .close1{
            width: 60px;
            height: 26px;
            border-radius: 15px;
            overflow: hidden;
            position: relative;
            background: #659dea;
        }
        .close2{
            left: 25px;
            top: 3px;
        }
    </style>
</head>
<body>
    <div class="div1 open1">
        <span class="left"></span>
        <span class="right"></span>
        <div class="div2 open2"></div>
    </div>
 
<script>
    $(function(){
        $(".div2").on('click',function() {
            // console.log('className======='+$(this).parent().get(0).className)
            $(this).parent().toggleClass('close1');
            $(this).parent().toggleClass('open1');
            $(this).toggleClass('close2');
            $(this).toggleClass('open2');
        })
    });
</script>
 
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值