单击内部菜单列表时,不应关闭可单击的下拉菜单

时间:2017-04-15 11:42:52

标签: javascript html drop-down-menu

我正在制作可点击的下拉菜单

  1. 当用户点击绿色下拉菜单按钮时,菜单将会打开
  2. 当用户再次点击绿色按钮时,菜单将关闭。
  3. 但是当下拉菜单打开并且用户在页面的任何位置点击下拉菜单之外时,菜单将再次关闭。
  4. 我已经设置了所有这3个步骤并且工作正常但我想要的是,当下拉菜单打开时,用户点击菜单区域(红色背景颜色区域),然后菜单不应该关闭。我只需要相同的JAVASCRIPT代码。即使您可以使用全新的JAVASCRIPT代码来完成它,也会有所帮助。

    
    
    /* 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');
          }
        }
      }
    }
    &#13;
    .dropbtn {
      background-color: #4CAF50;
      color: white;
      padding: 16px;
      font-size: 16px;
      border: none;
      cursor: pointer;
    }
    
    .dropbtn:hover,
    .dropbtn:focus {
      background-color: #3e8e41;
    }
    
    .dropdown {
      position: relative;
      display: inline-block;
    }
    
    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #f9f9f9;
      min-width: 160px;
      overflow: auto;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
      z-index: 1;
    }
    
    .dropdown-content a {
      color: black;
      padding: 12px 16px;
      text-decoration: none;
      display: block;
    }
    
    .dropdown a:hover {
      background-color: #f1f1f1
    }
    
    .show {
      display: block;
    }
    &#13;
    <p>Click on the button to open the dropdown menu.</p>
    <p>when click here menu will close</p>
    
    <div class="dropdown">
      <button onclick="myFunction()" class="dropbtn">Dropdown</button>
      <div id="myDropdown" class="dropdown-content" style="background:red;">
        <p>when click here menu should not close</p>
        <div id="test1">
          <p>when click here menu should not close</p>
        </div>
        <div id="test2">
          <div id="subtest1">
            <p>when click here menu should not close</p>
          </div>
          <div id="subtest1">
            <p>when click here menu should not close</p>
          </div>
        </div>
      </div>
    </div>
    <p>when click here menu will close</p>
    &#13;
    &#13;
    &#13;

2 个答案:

答案 0 :(得分:1)

您只需在下拉列表中添加点击事件并停止传播,以防止事件冒泡到窗口。因此,当使用单击下拉列表时,窗口对象永远不会捕获单击事件。

// Prevent event bubble up to window.
document.getElementById("myDropdown").addEventListener('click', function (event) {
    event.stopPropagation();
});

// Inline event also working well, if you prefer this style.
<div id="myDropdown" onclick="event.stopPropagation()" class="dropdown-content" style="background:red;">

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

// Prevent event bubble up to window.
document.getElementById("myDropdown").addEventListener('click', function (event) {
  event.stopPropagation();
});

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

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #f1f1f1}

.show {display:block;}
<p>Click on the button to open the dropdown menu.</p>
<p>when click here menu will close</p>
<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content" style="background:red;">
    
    <p>when click here menu should not close</p>
    <div id="test1">
      <p>when click here menu should not close</p>
    </div>
    <div id="test2">
      <div id="subtest1">
        <p>when click here menu should not close</p>
      </div>
      <div id="subtest1">
        <p>when click here menu should not close</p>
      </div>
    </div>
  </div>
</div>
<p>when click here menu will close</p>

答案 1 :(得分:0)

这是我能提出的最小的JavaScript ...... ; - )

.dropdown {
  position: relative;
  display: inline-block;
  outline: none;            /* add */
}

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
  background-color: #3e8e41;
}

.dropdown .dropdown-content {
  position: absolute;
  top:100%;            /* add */
  background-color: #f9f9f9;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px rgba(0,0,0, 0.2);
  
  transition: 0.3s;    /* add */
  visibility: hidden;  /* add */
  opacity:0;           /* add */
}

.dropdown:focus .dropdown-content { /* add all */
  outline: none;
  visibility: visible;
  opacity:1;
  transform: translateY(10px);
}
<p>Click on the button to open the dropdown menu.</p>

<div class="dropdown" tabindex="1"> <!-- set tabindex -->
  <a class="dropbtn">Dropdown</a> <!-- Don't use Button -->
  <div class="dropdown-content">
    <p>when click here menu should not close</p>
    <div id="test1">
      <p>when click here menu should not close</p>
    </div>
    <div id="test2">
      <div id="subtest1">
        <p>when click here menu should not close</p>
      </div>
      <div id="subtest1">
        <p>when click here menu should not close</p>
      </div>
    </div>
  </div>
</div>

Ups!..根本没有JS 所以基本上使用tabindex,不要使用<button>并使用CSS :focus