为了账号安全,请及时绑定邮箱和手机立即绑定

Javascript 中的导航栏下拉按钮调用第一个按钮

Javascript 中的导航栏下拉按钮调用第一个按钮

暮色呼如 2023-11-13 15:03:20
在我的导航栏中,第一个按钮可以工作,但下一个按钮不起作用。后续按钮仅调用第一个按钮。我还想让我的导航响应汉堡包图标。我无法感知可能的解决方案。我知道 Javascript 仅调用第一个按钮,我想Javascript按类调用后续按钮。我不知道如何解决这个问题。我浏览的一些解决方案,只有按钮,但我希望按钮在导航栏中可单击。和其他一些解决方案,在导航栏上有下拉菜单,但单击菜单本身时它们不会关闭,但在窗口中单击时会关闭
查看完整描述

2 回答

?
Helenr

TA贡献1780条经验 获得超3个赞

注1:删除重复的ID。多个元素切勿具有相同的 ID。

注2:使用 时document.getElementById("myDropdown"),只会返回一个元素(&第一个元素)。

注3:我们可以使用类来代替“ID”。请检查下面的javascript代码以显示相应的下拉列表。


//JS

function myFunction(event) {

  var clickedElement = event.target;

  console.log(clickedElement);

  if (clickedElement.nodeName != 'BUTTON') {

    clickedElement = clickedElement.parentElement;


  }

  var dropdownElement = clickedElement.nextElementSibling;

  dropdownElement.classList.add('tempClass'); //adding tempclass to avoid this in below loop


  var allOtherDropdowns = document.getElementsByClassName('dropdown-content');


  //close all other dropdowns

  for (var i = 0; i < allOtherDropdowns.length; i++) {

    if (!allOtherDropdowns[i].classList.contains('tempClass')) {

      allOtherDropdowns[i].classList.remove('show');

    }

  }

  dropdownElement.classList.toggle("show");

  dropdownElement.classList.remove('tempClass'); //removing the temp class here 

}

//HTML

<div class="dropdown">

      <button class="dropbtn" onclick="myFunction(event)">Upgrade 2

          <i class="fa fa-caret-down"></i>

      </button>

      <div class="dropdown-content">

        <a href="#" onclick="myFunc();">item 1</a>

        <a href="#">item 2</a>

        <a href="#">item 3</a>

      </div>

</div>


function myFunc() {

  alert('You clicked a submenu item')

}


/* When the user clicks on the button, 

toggle between hiding and showing the dropdown content */

function myFunction(event) {

  var clickedElement = event.target;

  console.log(clickedElement);

  if (clickedElement.nodeName != 'BUTTON') {

    clickedElement = clickedElement.parentElement;


  }

  var dropdownElement = clickedElement.nextElementSibling;

  dropdownElement.classList.add('tempClass'); //adding tempclass to avoid this in below loop


  var allOtherDropdowns = document.getElementsByClassName('dropdown-content');


  //close all other dropdowns

  for (var i = 0; i < allOtherDropdowns.length; i++) {

    if (!allOtherDropdowns[i].classList.contains('tempClass')) {

      allOtherDropdowns[i].classList.remove('show');

    }

  }

  dropdownElement.classList.toggle("show");

  dropdownElement.classList.remove('tempClass'); //removing the temp class here

}


// Close the dropdown if the user clicks outside of it

window.onclick = function(e) {

  if (!e.target.matches('.dropbtn')) {

    var allDropdowns = document.getElementsByClassName('dropdown-content');


    //close all other dropdowns

    for (var i = 0; i < allDropdowns.length; i++) {

      allDropdowns[i].classList.remove('show');

    }

  }

}

.navbar {

  overflow: hidden;

  background-color: #333;

  font-family: Arial, Helvetica, sans-serif;

}


.navbar a {

  float: left;

  font-size: 16px;

  color: white;

  text-align: center;

  padding: 14px 16px;

  text-decoration: none;

}


.dropdown {

  float: left;

  overflow: hidden;

}


.dropdown .dropbtn {

  cursor: pointer;

  font-size: 16px;

  border: none;

  outline: none;

  color: white;

  padding: 14px 16px;

  background-color: inherit;

  font-family: inherit;

  margin: 0;

}


.navbar a:hover,

.dropdown:hover .dropbtn,

.dropbtn:focus {

  background-color: red;

}


.dropdown-content {

  display: none;

  position: absolute;

  background-color: #f9f9f9;

  min-width: 160px;

  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);

  z-index: 1;

}


.dropdown-content a {

  float: none;

  color: black;

  padding: 12px 16px;

  text-decoration: none;

  display: block;

  text-align: left;

}


.dropdown-content a:hover {

  background-color: #ddd;

}


.show {

  display: block;

}

<!DOCTYPE html>

<html>


<head>

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">


</head>


<body>


  <div class="navbar">

    <div class="dropdown">

      <button class="dropbtn" onclick="myFunction(event)">Upgrade 1

          <i class="fa fa-caret-down"></i>

      </button>

      <div class="dropdown-content">

        <a href="#" onclick="myFunc();">item 1</a>

        <a href="#">item 2</a>

        <a href="#">item 3</a>

      </div>

    </div>



    <div class="dropdown">

      <button class="dropbtn" onclick="myFunction(event)">Upgrade 2

          <i class="fa fa-caret-down"></i>

      </button>

      <div class="dropdown-content">

        <a href="#" onclick="myFunc();">item 1</a>

        <a href="#">item 2</a>

        <a href="#">item 3</a>

      </div>

    </div>


    <div class="dropdown">

      <button class="dropbtn" onclick="myFunction(event)">Upgrade 3

          <i class="fa fa-caret-down"></i>

      </button>

      <div class="dropdown-content">

        <a href="#" onclick="myFunc();">item 1</a>

        <a href="#">item 2</a>

        <a href="#">item 3</a>

      </div>

    </div>



  </div>


  <h3>Dropdown Menu inside a Navigation Bar</h3>

  <p>Click on the "Dropdown" link to see the dropdown menu.</p>


</body>


</html>


查看完整回答
反对 回复 2023-11-13
?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

  function myFunction() {

        document.getElementById("myDropdown").classList.toggle("show");

        document.getElementById("myDropdown1").classList.remove("show");

        document.getElementById("myDropdown2").classList.remove("show");

    }


    function myFunction1() {

        document.getElementById("myDropdown1").classList.toggle("show");

        document.getElementById("myDropdown").classList.remove("show");

        document.getElementById("myDropdown2").classList.remove("show");

    }


    function myFunction2() {

        document.getElementById("myDropdown2").classList.toggle("show");

        document.getElementById("myDropdown1").classList.remove("show");

        document.getElementById("myDropdown").classList.remove("show");

    }



    // Close the dropdown if the user clicks outside of it

    window.onclick = function(e) {

        if (!e.target.matches('.dropbtn')) {

            var myDropdown = document.getElementById("myDropdown");

            var myDropdown1 = document.getElementById("myDropdown1");

            var myDropdown2 = document.getElementById("myDropdown2");

            if (myDropdown.classList.contains('show')) {

                myDropdown.classList.remove('show');

            }

            if (myDropdown1.classList.contains('show')) {

                myDropdown1.classList.remove('show');

            }

            if (myDropdown2.classList.contains('show')) {

                myDropdown2.classList.remove('show');

            }

        }

    }

我认为相同的 Id 是问题所在


查看完整回答
反对 回复 2023-11-13
  • 2 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信