javascript - 【求救】子开关全部开启或者关闭,总开关联动起来
ringa_lee
ringa_lee 2017-04-11 09:51:15
0
1
428

子开关全部开启或者关闭,总开关联动开或者关,最后一个为总开关,图片在底部。谢!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>开关</title>
    <style type="text/css">
        *{
            margin:0;
            padding:0;
        }
        .switch{
            float: left;
        }
        .swich{
            width:41px;
            height:47px;
            margin:20px;
            cursor: pointer;
        }
        .on,.off{
            width:41px;
            height:23px;
            background: url(icon.png) no-repeat;
        }
        .on{
            background-position: 0 0;
        }
        .off{
            background-position: 0 -24px;
        }
    </style>
</head>
<body>
<p class="switch">
    <p class="all">
        <p class="swich off"></p>
        <p class="swich off"></p>
        <p class="swich on"></p>
        <p class="swich on"></p>
        <p class="swich on"></p>
        <p class="swich off"></p>
        <p class="swich on"></p>
        <p class="swich on"></p>
    </p>
    <p class="swich off swichAll"></p>
</p>
<p class="switch">
    <p class="all">
        <p class="swich off"></p>
        <p class="swich on"></p>
        <p class="swich off"></p>
        <p class="swich off"></p>
        <p class="swich on"></p>
        <p class="swich off"></p>
        <p class="swich off"></p>
        <p class="swich on"></p>
    </p>
    <p class="swich off swichAll"></p>
</p>
<p class="switch">
    <p class="all">
        <p class="swich on"></p>
        <p class="swich on"></p>
        <p class="swich off"></p>
        <p class="swich on"></p>
        <p class="swich on"></p>
        <p class="swich off"></p>
        <p class="swich on"></p>
        <p class="swich off"></p>
    </p>
    <p class="swich off swichAll"></p>
</p>
    <script type="text/javascript" src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            // 开关控制
            $(".swich").click(function(){
                if($(this).hasClass("off")){
                    $(this).attr("class","swich on");
                }else{
                    $(this).attr("class","swich off");
                }
            });

            // 开关全选/反选
            $(".swichAll").click(function(){
                if($(this).hasClass("off")){
                    $(this).prev().children("p").attr("class","swich off");
                }else{
                    $(this).prev().children("p").attr("class","swich on");
                }
            });
        });
    </script>
</body>
</html>

ringa_lee
ringa_lee

ringa_lee

全部回复(1)
黄舟

很久没写这种强耦合的面条代码了

// 开关控制
$(".swich").click(function() {
  var ele = $(this);
  var hassOff = ele.hasClass("off");
  ele.attr("class", "swich " + (hassOff ? "on" : "off"));
  ele.parent().siblings('.swichAll').attr("class", "swichAll swich " + (hassOff && ele.siblings('.swich.off').length === 0 ? "on" : "off"))
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!