簡體   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