繁体   English   中英

如何使JavaScript仅影响某个div中的元素?

[英]How can I make JavaScript only affect elements within a certain div?

我有一个选择/取消选择所有复选框的脚本,如果我选中一个特定的复选框,将选中所有复选框。 但是我希望它能在各节中工作,因此只有第1节中的复选框会受到该节中所有复选框的影响。 甚至id都可以。 以任何方式仅影响同一section标记中的复选框。

当前脚本:

$("#DE-all-checkboxes").change(function () {
    $("input:checkbox").prop('checked', $(this).prop("checked"));
});
<html>
    <head>
        <script
              src="https://code.jquery.com/jquery-3.1.1.min.js"
              integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
              crossorigin="anonymous"></script>

    </head>
    <body>
    Check Uncheck All In Section 1 <input id="DE-all-checkboxes" type="checkbox"/>          
        <div id="section1"> 
            Section 1:
            <input type="checkbox"/>
            <input type="checkbox"/>
            <input type="checkbox"/>
            <input type="checkbox"/>
            <input type="checkbox"/>
        </div>
        <div id="section2">         
            Section 2:
            <input type="checkbox"/>
            <input type="checkbox"/>
            <input type="checkbox"/>
            <input type="checkbox"/>
            <input type="checkbox"/>
        </div>
        <script>
            $("#DE-all-checkboxes").change(function () {            
                $("#section1 > input:checkbox").prop('checked', $(this).prop("checked"));
            });
        </script>
    </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM