简体   繁体   中英

Clicking drop down opens the wrong dtop down

I have a drop like this

<?php
            foreach($AllPosts as $post)
            {
                //echo $post['message'];
                echo "<br/>";


        ?>

        <div>
            <div class="dropdown">
            <div onclick='myFunction();' class="test dropbtn"></div>
              <div id="myDropdown" class="dropdown-content">
                <a href="#home">Message</a>
                <a href="#about">Comment</a>
              </div>
            </div>

            <div>
                <p><?php echo $post['message']; ?></p>
            </div>
        </div>

        <?php } ?>

and this is the javascript

<script>
        /* When the user clicks on the button, 
        toggle between hiding and showing the dropdown content */
        function myFunction() {
            document.getElementById("myDropdown").classList.toggle("show");
        }

        // Close the dropdown if the user clicks outside of it
        window.onclick = function(event) {
          if (!event.target.matches('.dropbtn')) {

            var dropdowns = document.getElementsByClassName("dropdown-content");
            var i;
            for (i = 0; i < dropdowns.length; i++) {
              var openDropdown = dropdowns[i];
              if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
              }
            }
          }
        }
        </script>

when in the foreach the first one works fine but as soon as you click the second one it opens the first one again instead not not sure whats wrong

I am not sure what you are trying to do, but you can try this one:

<?php
$i = 0;
foreach($AllPosts as $post)
{
    //echo $post['message'];
    echo "<br/>";


    ?>

    <div>
        <div class="dropdown">
            <div onclick='myFunction("myDropdown" + <?php echo $i?>);' class="test dropbtn">sdfgsd</div>
            <div id="myDropdown<?php echo $i?>" class="dropdown-content">
                <a href="#home">Message</a>
                <a href="#about">Comment</a>
            </div>
        </div>

        <div>
            <p><?php echo $post; ?></p>
        </div>
    </div>

<?php $i++; } ?>



<script>
    /* When the user clicks on the button, 
     toggle between hiding and showing the dropdown content */
    function myFunction(e) {
        document.getElementById(e).classList.toggle("show");
    }

    // Close the dropdown if the user clicks outside of it
    window.onclick = function(event) {
        if (!event.target.matches('.dropbtn')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            let i;
            for (i = 0; i < dropdowns.length; i++) {
                var openDropdown = dropdowns[i];
                openDropdown.classList.remove('show');
            }
        }
    }
</script>

EDIT: Another way:

<?php
$AllPosts = ["aaa", "bbb", "ccc"];
$i = 0;
foreach($AllPosts as $post)
{
    //echo $post['message'];
    echo "<br/>";


    ?>

    <div>
        <div class="dropdown">
            <div onclick='this.nextElementSibling.classList.toggle("show")' class="test dropbtn">sdfgsd</div>
            <div id="myDropdown<?php echo $i?>" class="dropdown-content">
                <a href="#home">Message</a>
                <a href="#about">Comment</a>
            </div>
        </div>

        <div>
            <p><?php echo $post; ?></p>
        </div>
    </div>

<?php $i++; } ?>



<script>
    // Close the dropdown if the user clicks outside of it
    window.onclick = function(event) {
        if (!event.target.matches('.dropbtn')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            let i;
            for (i = 0; i < dropdowns.length; i++) {
                var openDropdown = dropdowns[i];
                openDropdown.classList.remove('show');
            }
        }
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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