亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

導航欄上方有空白

導航欄上方有空白

慕少森 2023-11-13 14:44:20
我正在設計一個網站,因為我正在向其添加可點擊的按鈕。我的導航欄上面有一些空白。以前我曾發布過同樣的問題,但現在我不知道我的CSS出了什么問題。請糾正或指出我的 CSS 哪里做錯了。我不想渲染我的導航欄,其上方有空白。所以,請更改 CSS,并告訴我哪里出錯了。我還添加了 css 圖標。正如我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 itwindow.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');    }  }}
查看完整描述

1 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

你的h3文字Dropdown Menu inside a Navigation Bar有一個邊距。而且你的身體也有很小的余量?,F在應該好了。


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

    }

  }

}

function myFunc(){return true}

h3,body,html{

  margin:0;

  padding:0;

}

i {

  width: 0;

  height: 0;

  border-style: solid;

  border-width: 8px 5px 0 5px;

  border-color: #ffffff transparent transparent transparent;

}


.icon {

  position: relative;

  /* Adjust these values accordingly */

  top: 14px;

  left: 9px;

}


nav {

  height: 40px overflow: hidden;

  background-color: #333;

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

}


nav a {

  float: left;

  font-size: 16px;

  color: white;

  text-align: center;

  padding: 14px 16px;

  text-decoration: none;

}


.dropdown {

  float: left;

  overflow: hidden;

}


.drop-btn {

  float: right;

}


.dropdown .dropbtn {

  cursor: pointer;

  font-size: 14px;

  border: none;

  outline: none;

  color: white;

  padding: 10px 10px;

  background-color: inherit;

  font-family: inherit;

  margin: 0;

}


nav 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">


</head>


<body>


  <nav>

    <div class="dropdown">

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

          <i class="icon"></i>

      </button>

      <div class="dropdown-content">

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

        <a href="#">C++</a>

        <a href="#">Python</a>

        <a href="#">Swift</a>

      </div>

    </div>





    <div class="dropdown">

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

          <i class="icon"></i>

      </button>

      <div class="dropdown-content">

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

        <a href="#">Windows</a>

        <a href="#">Development Editor</a>

      </div>

    </div>


    <div class="dropdown">

      <button class="dropbtn">Algorithm

          

      </button>


    </div>



    <div class="dropdown">

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

          <i class="icon"></i>

      </button>

      <div class="dropdown-content">

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

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

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

      </div>

    </div>



    <div class="dropdown">

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

          <i class="icon"></i>

      </button>

      <div class="dropdown-content">

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

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

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

      </div>

    </div>



    <div class="dropdown">

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

          <i class="icon"></i>

      </button>

      <div class="dropdown-content">

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

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

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

      </div>

    </div>



    <nav>


      <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
  • 1 回答
  • 0 關注
  • 120 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號