打开另一个时,导航下降不会关闭

问题描述 投票:0回答:3

这几乎按预期工作,点击屏幕时下拉确实关闭,但打开新下拉时它们保持打开状态。我希望它能打开一个新的关闭开放下拉。为了使它能够做到,我需要在脚本中进行哪些更改?先感谢您。

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

function myFunction2(event) {
  event.stopPropagation();
  document.getElementById("myDropdown2").classList.toggle("show");
}

function myFunction3(event) {
  event.stopPropagation();
  document.getElementById("myDropdown3").classList.toggle("show");
}



// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown2").classList.remove("show");
  document.getElementById("myDropdown3").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);
}


/* Links inside the dropdown */

.dropdown-content a {
  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;
}


/* Dropdown Button */

.dropbtn2 {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}


/* Dropdown button on hover & focus */

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


/* The container <div> - needed to position the dropdown content */

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


/* Dropdown Content (Hidden by Default) */

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


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

.dropdown-content2 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;
}


/* Dropdown Button */

.dropbtn3 {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}


/* Dropdown button on hover & focus */

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


/* The container <div> - needed to position the dropdown content */

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


/* Dropdown Content (Hidden by Default) */

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


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

.dropdown-content3 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;
}
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">



</head>

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
  </script>
  <table>
    <tr>
      <td>
        <div class="dropdown">
          <button onclick="myFunction(event)" class="dropbtn">Dropdown</button>
          <div id="myDropdown" class="dropdown-content">
            <a href="#">Link 1</a>
            <a href="#">Link 2</a>
            <a href="#">Link 3</a>
          </div>
        </div>
      </td>
      <td>
        <div class="dropdown2">
          <button onclick="myFunction2(event)" class="dropbtn2">Dropdown2</button>
          <div id="myDropdown2" class="dropdown-content2">
            <a href="#">Link 4</a>
            <a href="#">Link 5</a>
            <a href="#">Link 6</a>
          </div>
        </div>

      </td>
      <td>
        <div class="dropdown3">
          <button onclick="myFunction3(event)" class="dropbtn3">Dropdown3</button>
          <div id="myDropdown3" class="dropdown-content3">
            <a href="#">Link 7</a>
            <a href="#">Link 8</a>
            <a href="#">Link 9</a>
          </div>
        </div>

      </td>
    </tr>
  </table>

</body>

</html>
javascript jquery drop-down-menu navigation
3个回答
0
投票

您可以在myFunctions中使用classList.remove从其他div中删除类show

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

function myFunction2(event) {
  event.stopPropagation();
  document.getElementById("myDropdown2").classList.toggle("show");
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown3").classList.remove("show");
}

function myFunction3(event) {
  event.stopPropagation();
  document.getElementById("myDropdown3").classList.toggle("show");
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown2").classList.remove("show");
}



// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown2").classList.remove("show");
  document.getElementById("myDropdown3").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);
}


/* Links inside the dropdown */

.dropdown-content a {
  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;
}


/* Dropdown Button */

.dropbtn2 {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}


/* Dropdown button on hover & focus */

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


/* The container <div> - needed to position the dropdown content */

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


/* Dropdown Content (Hidden by Default) */

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


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

.dropdown-content2 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;
}


/* Dropdown Button */

.dropbtn3 {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}


/* Dropdown button on hover & focus */

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


/* The container <div> - needed to position the dropdown content */

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


/* Dropdown Content (Hidden by Default) */

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


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

.dropdown-content3 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;
}
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">



</head>

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
  </script>
  <table>
    <tr>
      <td>
        <div class="dropdown">
          <button onclick="myFunction(event)" class="dropbtn">Dropdown</button>
          <div id="myDropdown" class="dropdown-content">
            <a href="#">Link 1</a>
            <a href="#">Link 2</a>
            <a href="#">Link 3</a>
          </div>
        </div>
      </td>
      <td>
        <div class="dropdown2">
          <button onclick="myFunction2(event)" class="dropbtn2">Dropdown2</button>
          <div id="myDropdown2" class="dropdown-content2">
            <a href="#">Link 4</a>
            <a href="#">Link 5</a>
            <a href="#">Link 6</a>
          </div>
        </div>

      </td>
      <td>
        <div class="dropdown3">
          <button onclick="myFunction3(event)" class="dropbtn3">Dropdown3</button>
          <div id="myDropdown3" class="dropdown-content3">
            <a href="#">Link 7</a>
            <a href="#">Link 8</a>
            <a href="#">Link 9</a>
          </div>
        </div>

      </td>
    </tr>
  </table>

</body>

</html>

0
投票

试试这个。

function myFunction(event) {
  event.stopPropagation();
  document.getElementById("myDropdown2").classList.remove("show");
  document.getElementById("myDropdown3").classList.remove("show");
  document.getElementById("myDropdown").classList.toggle("show");
}

function myFunction2(event) {
  event.stopPropagation();
  document.getElementById("myDropdown3").classList.remove("show");
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown2").classList.toggle("show");
}

function myFunction3(event) {
  event.stopPropagation();
  document.getElementById("myDropdown2").classList.remove("show");
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown3").classList.toggle("show");
}



// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown2").classList.remove("show");
  document.getElementById("myDropdown3").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);
}
/* Links inside the dropdown */

.dropdown-content a {
  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;
}
/* Dropdown Button */

.dropbtn2 {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}
/* Dropdown button on hover & focus */

.dropbtn2:hover,
.dropbtn2:focus {
background-color: #3e8e41;
}
/* The container <div> - needed to position the dropdown content */

.dropdown2 {
  position: relative;
  display: inline-block;
}
/* Dropdown Content (Hidden by Default) */

.dropdown-content2 {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
/* Links inside the dropdown */

.dropdown-content2 a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}
/* Change color of dropdown links on hover */

.dropdown-content2 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;
}

/* Dropdown Button */

.dropbtn3 {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}
/* Dropdown button on hover & focus */

.dropbtn3:hover,
.dropbtn3:focus {
  background-color: #3e8e41;
}
/* The container <div> - needed to position the dropdown content */

.dropdown3 {
  position: relative;
  display: inline-block;
}
/* Dropdown Content (Hidden by Default) */

.dropdown-content3 {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
/* Links inside the dropdown */

.dropdown-content3 a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}
/* Change color of dropdown links on hover */

.dropdown-content3 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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <div class="dropdown">
        <button onclick="myFunction(event)" class="dropbtn">Dropdown</button>
        <div id="myDropdown" class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </div>
    </td>
    <td>
      <div class="dropdown2">
        <button onclick="myFunction2(event)" class="dropbtn2">Dropdown2</button>
        <div id="myDropdown2" class="dropdown-content2">
          <a href="#">Link 4</a>
          <a href="#">Link 5</a>
          <a href="#">Link 6</a>
        </div>
      </div>
    </td>
    <td>
      <div class="dropdown3">
        <button onclick="myFunction3(event)" class="dropbtn3">Dropdown3</button>
        <div id="myDropdown3" class="dropdown-content3">
          <a href="#">Link 7</a>
          <a href="#">Link 8</a>
          <a href="#">Link 9</a>
        </div>
      </div>
    </td>
  </tr>
</table>

0
投票

创建一个简单的函数,清除所有下拉列表,并在显示每个下拉列表之前触发它,以及单击窗口。

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

  function myFunction2(event) {
  event.stopPropagation();
   closeDropdowns();
  document.getElementById("myDropdown2").classList.toggle("show");
}

function myFunction3(event) {
  event.stopPropagation();
  closeDropdowns();
  document.getElementById("myDropdown3").classList.toggle("show");
}

function closeDropdowns(){
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown2").classList.remove("show");
  document.getElementById("myDropdown3").classList.remove("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  closeDropdowns();
} 
© www.soinside.com 2019 - 2024. All rights reserved.