HTML下拉列表无法正常工作

时间:2017-06-01 12:24:06

标签: javascript html css

我有一个无法正常工作的下拉菜单。当我单击其中一个按钮时,将出现一个下拉菜单。但当我再次点击按钮时,它应该关闭,但它不会。我知道它与closeAll函数有关,但是当第一个函数已经打开时,我仍然需要该函数来关闭另一个下拉菜单。

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

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

    function closeAll(){
	    var dropdowns = document.getElementsByClassName("dropdown-content");
	    var i;
	    for (i = 0; i < dropdowns.length; i++) {
	     var openDropdown = dropdowns[i];
	      openDropdown.classList.remove('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: 150px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
    }

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

    /* Change color of dropdown links on hover */
    #myDropdown 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;}

    #myDropdown2{
	    min-width:200px;
	    border:4px solid red;
	
    }

    #myDropdown2 a:hover{
	    color:red;
    }

    .left-bar{
	    float:left;
    }

    .right-bar{
	    float:left;
    }
    

    <div class="dropdown">
      <button onclick="myFunction()" 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>

    <div class="dropdown">
      <button onclick="myFunction2()" class="dropbtn">Dropdown</button>
      <div id="myDropdown2" class="dropdown-content">
	    <div class="left-bar">
		    <a href="#">Link 1</a>
		    <a href="#">Link 2</a>
		    <a href="#">Link 3</a>
	    </div>
	
	    <div class="right-bar">
		    <a href="#">Link 1</a>
		    <a href="#">Link 2</a>
		    <a href="#">Link 3</a>
	    </div>
      </div>
    </div>

5 个答案:

答案 0 :(得分:3)

  

您的功能(myFunction() and myFunction2())首先使用closeAll();关闭所有下拉菜单,下一行document.getElementById("myDropdown").classList.toggle("show");再次显示它。所以,当您再次点击该按钮时و它不会关闭。

     

修复它:

     

只需删除功能closeAll();并更改您的代码,例如:

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

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

完整代码:

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

function myFunction2() {
  document.getElementById("myDropdown").classList.remove("show");
  document.getElementById("myDropdown2").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');

      }
    }
  }
}
.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: 150px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

#myDropdown 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;
}

#myDropdown2 {
  min-width: 200px;
  border: 4px solid red;
}

#myDropdown2 a:hover {
  color: red;
}

.left-bar {
  float: left;
}

.right-bar {
  float: left;
}
<div class="dropdown">
  <button onclick="myFunction()" 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>

<div class="dropdown">
  <button onclick="myFunction2()" class="dropbtn">Dropdown</button>
  <div id="myDropdown2" class="dropdown-content">
    <div class="left-bar">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>

    <div class="right-bar">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>

答案 1 :(得分:-1)

myFunction()&amp;移除此功能myFunction2() closeAll();

您已使用ToggleClass

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

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

function closeAll() {
  var dropdowns = document.getElementsByClassName("dropdown-content");
  var i;
  for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];
    openDropdown.classList.remove('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: 150px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

#myDropdown 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;
}

#myDropdown2 {
  min-width: 200px;
  border: 4px solid red;
}

#myDropdown2 a:hover {
  color: red;
}

.left-bar {
  float: left;
}

.right-bar {
  float: left;
}
<div class="dropdown">
  <button onclick="myFunction()" 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>

<div class="dropdown">
  <button onclick="myFunction2()" class="dropbtn">Dropdown</button>
  <div id="myDropdown2" class="dropdown-content">
    <div class="left-bar">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>

    <div class="right-bar">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>

答案 2 :(得分:-1)

在第一步中关闭所有下拉列表,然后切换当前的下拉列表,然后重新打开。如果要更改此行为,则必须更改closeAll()函数以接收一个参数,该参数将是当前下拉列表或按钮,并且仅关闭除此之外的所有参数。

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

您可以保存元素在数组中应具有的状态,并在每次单击时应用所有这些状态。

// executed once on page load
var dropdowns = new array();
var tmpdropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < tmpdropdowns.length; i++) {
    dropdowns[tmpdropdowns[i].id] = 0;
}


// executed on click of one button
for (i = 0; i < dropdowns.length; i++) {
    dropdowns[i] = 0;
}
dropdowns[/*clicked id*/] = (dropdowns[/*clicked id*/] + 1) % 2;
for (var key in dropdowns) {
   if(dropdowns[key] == 0) {
       document.getElementById[key].classList.remove("show");
   } else {
       document.getElementById[key].classList.add("show");
   }
}

答案 3 :(得分:-1)

问题在于,在您致电closeAll()后,您切换了用户点击的下拉列表,再次打开该下拉菜单。

您需要测试当前的一个是否已经打开,而不是重新打开它。

&#13;
&#13;
/* When the user clicks on the button, 
    toggle between hiding and showing the dropdown content */
function myFunction() {
  var isOpen = document.getElementById("myDropdown").classList.contains("show");
  closeAll();
  if (!isOpen) {
    document.getElementById("myDropdown").classList.add("show");
  }
}

function myFunction2() {
  var isOpen = document.getElementById("myDropdown2").classList.contains("show");
  closeAll();
  if (!isOpen) {
    document.getElementById("myDropdown2").classList.toggle("show");
  }
}

function closeAll() {
  var dropdowns = document.getElementsByClassName("dropdown-content");
  var i;
  for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];
    openDropdown.classList.remove('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');

      }
    }
  }
}
&#13;
/* 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: 150px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

#myDropdown 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;
}

#myDropdown2 {
  min-width: 200px;
  border: 4px solid red;
}

#myDropdown2 a:hover {
  color: red;
}

.left-bar {
  float: left;
}

.right-bar {
  float: left;
}
&#13;
<div class="dropdown">
  <button onclick="myFunction()" 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>

<div class="dropdown">
  <button onclick="myFunction2()" class="dropbtn">Dropdown</button>
  <div id="myDropdown2" class="dropdown-content">
    <div class="left-bar">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>

    <div class="right-bar">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:-1)

尝试此操作:当点击show元素的hasClass时,删除另一个下拉菜单的课程show。因为每次点击closeAll都会删除在所有下拉列表中show。因此,您只能删除show元素的hasClass。它会阻止dropdown的切换事件

Array.prototype.indexOf.call(document.getElementById("myDropdown").classList, 'show')

用于查找hasClass

/* When the user clicks on the button, 
    toggle between hiding and showing the dropdown content */
function myFunction() {
if(Array.prototype.indexOf.call(document.getElementById("myDropdown").classList, 'show')){
  document.getElementById("myDropdown").classList.toggle("show");
   document.getElementById("myDropdown2").classList.remove("show");
  }
  else{
  closeAll();
  }
  
}

function myFunction2() {if(Array.prototype.indexOf.call(document.getElementById("myDropdown2").classList, 'show')){
  document.getElementById("myDropdown2").classList.toggle("show");
     document.getElementById("myDropdown").classList.remove("show");
  }
  else{
  closeAll()
  }
  
}

function closeAll() {
  var dropdowns = document.getElementsByClassName("dropdown-content");
  var i;
  for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];
    openDropdown.classList.remove('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: 150px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

#myDropdown 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;
}

#myDropdown2 {
  min-width: 200px;
  border: 4px solid red;
}

#myDropdown2 a:hover {
  color: red;
}

.left-bar {
  float: left;
}

.right-bar {
  float: left;
}
<div class="dropdown">
  <button onclick="myFunction()" 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>

<div class="dropdown">
  <button onclick="myFunction2()" class="dropbtn">Dropdown</button>
  <div id="myDropdown2" class="dropdown-content">
    <div class="left-bar">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>

    <div class="right-bar">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>