第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 中的導(dǎo)航欄下拉按鈕調(diào)用第一個按鈕

Javascript 中的導(dǎo)航欄下拉按鈕調(diào)用第一個按鈕

暮色呼如 2023-11-13 15:03:20
在我的導(dǎo)航欄中,第一個按鈕可以工作,但下一個按鈕不起作用。后續(xù)按鈕僅調(diào)用第一個按鈕。我還想讓我的導(dǎo)航響應(yīng)漢堡包圖標(biāo)。我無法感知可能的解決方案。我知道 Javascript 僅調(diào)用第一個按鈕,我想Javascript按類調(diào)用后續(xù)按鈕。我不知道如何解決這個問題。我瀏覽的一些解決方案,只有按鈕,但我希望按鈕在導(dǎo)航欄中可單擊。和其他一些解決方案,在導(dǎo)航欄上有下拉菜單,但單擊菜單本身時它們不會關(guān)閉,但在窗口中單擊時會關(guān)閉
查看完整描述

2 回答

?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗 獲得超4個贊

注1:刪除重復(fù)的ID。多個元素切勿具有相同的 ID。

注2:使用 時document.getElementById("myDropdown"),只會返回一個元素(&第一個元素)。

注3:我們可以使用類來代替“ID”。請檢查下面的javascript代碼以顯示相應(yīng)的下拉列表。


//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>


查看完整回答
反對 回復(fù) 2023-11-13
?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗 獲得超8個贊

  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');

            }

        }

    }

我認(rèn)為相同的 Id 是問題所在


查看完整回答
反對 回復(fù) 2023-11-13
  • 2 回答
  • 0 關(guān)注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號