【问题标题】:How To Prevent Drop Down Menu From Closing When User Clicks Into It?如何防止用户点击下拉菜单时关闭它?
【发布时间】:2016-10-26 12:49:12
【问题描述】:

我在 W3Schools 网站上关注了tutorial,关于从按钮制作下拉菜单。一切正常,除了当用户在下拉菜单中单击时,菜单本身就会消失。

我想制作一个登录按钮,用户单击该按钮,它会显示一个下拉菜单,他/她可以在其中输入他/她的用户名和密码,然后单击登录。

我想保留该功能,如果用户单击下拉菜单外部的任何其他区域,它将自动关闭下拉菜单。

我的问题是,当用户点击内部的任何区域时,下拉菜单会消失。在仅使用 JavaScript 的下拉菜单中单击时,如何防止它消失?使用 jQuery 怎么样(我对 jQuery 还不是很熟悉)

/* 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 menu 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');
      }
    }
  }
}
/* Dropdown Button */
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Contents inside the dropdown */
.dropdown-content input, .dropdown-content button {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">LOG IN</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" name="username" placeholder="Enter username"/>
    <input type="text" name="password" placeholder="Enter password"/>
    <button type="submit" name="submit">Submit</button>
  </div>
</div>

【问题讨论】:

  • 页面上是否只有一个下拉菜单?
  • 我已经在
  • 所以在里面点击时应该关闭它们?
  • 对于其他下拉菜单,我可以在点击内部时关闭它们,因为它们只是链接。对于这个,它是不同的,因为它是一个登录功能,用户必须点击里面,下拉实际上必须保持打开状态,而用户输入他们的信息
  • 这是该页面上唯一具有此功能的。

标签: javascript jquery css button drop-down-menu


【解决方案1】:

在 window.onClick 处理程序上,我们添加一个条件来排除“下拉内容”及其子项。

/* 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 menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    // [BEGIN][Here is the condition]
    if (event.target.matches('.dropdown-content') || event.target.matches('.dropdown-content *') ) {
       event.stopPropagation();
       return;
    }
  // [END][Here is the condition]
    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');
      }
    }
  }
}
/* Dropdown Button */
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Contents inside the dropdown */
.dropdown-content input, .dropdown-content button {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">LOG IN</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" name="username" placeholder="Enter username"/>
    <input type="text" name="password" placeholder="Enter password"/>
    <button type="submit" name="submit">Submit</button>
  </div>
</div>

【讨论】:

    【解决方案2】:

    当您点击内部下拉菜单时, 你不应该删除表演类

    /* 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 menu if the user clicks outside of it
    window.onclick = function(event) {
       
      if (event.target.matches('.dropdown-content *')) {
        return;
       } 
    
      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');
          }
        }
      }
    }
    /* Dropdown Button */
    .dropbtn {
        background-color: #4CAF50;
        color: white;
        padding: 16px;
        font-size: 16px;
        border: none;
        cursor: pointer;
    }
    
    /* Dropdown button on hover & focus */
    .dropbtn:hover, .dropbtn:focus {
        background-color: #3e8e41;
    }
    
    /* The container <div> - needed to position the dropdown content */
    .dropdown {
        position: relative;
        display: inline-block;
    }
    
    /* Dropdown Content (Hidden by Default) */
    .dropdown-content {
        display: none;
        position: absolute;
        background-color: #f9f9f9;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    }
    
    /* Contents inside the dropdown */
    .dropdown-content input, .dropdown-content button {
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
    }
    
    /* Change color of dropdown links on hover */
    .dropdown-content a:hover {background-color: #f1f1f1}
    
    /* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
    .show {display:block;}
    <div class="dropdown">
      <button onclick="myFunction()" class="dropbtn">LOG IN</button>
      <div id="myDropdown" class="dropdown-content">
        <input type="text" class="form-input" name="username" placeholder="Enter username"/>
        <input type="text" class="form-input" name="password" placeholder="Enter password"/>
        <button type="submit" name="submit">Submit</button>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2014-04-28
      • 2018-02-16
      • 1970-01-01
      • 2019-08-22
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      相关资源
      最近更新 更多