点击下拉菜单会打开错误的dtop下拉菜单

时间:2018-07-13 21:54:46

标签: javascript html

我有这样的下落

<?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 } ?>

这是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>

在foreach中,第一个可以正常工作,但是一旦您单击第二个,它就会再次打开第一个,而不是不确定什么地方出错

1 个答案:

答案 0 :(得分:1)

我不确定您要做什么,但是您可以尝试以下操作:

<?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>

编辑:另一种方式:

<?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>