簡體   English   中英

HTML下拉菜單文本不出現

[英]HTML Dropdown Menu text does not appear

您好,我對html下拉菜單有疑問。

我只是按照w3學校的指示創建下拉菜單。 https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown

一切正常,但是當我單擊菜單按鈕時,無法看到如下菜單列表。 有什么我可以解決的問題嗎?

沒有過濾器菜單

 function dropDownMenu() { document.getElementById("myDropdown").classList.toggle("show"); } // Close the dropdown 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: #3498DB; color: white; padding: 13px; font-size: 13px; border: none; position: absolute; right: -490px; top: 8px; background-color: #0076B1; border-radius: 25%; cursor: pointer; transition: 0.3s; } .dropbtn:hover, .dropbtn:focus { background-color: #2980B9; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: white; min-width: 160px; overflow: auto; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; right: -490px; top: 52px; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown a:hover {background-color: #ddd} .show {display:block;} 
 <div class="dropdown"> <button onclick="dropDownMenu()" class="dropbtn">Change Date Filter</button> <div id="myDropdown" class="dropdown-content"> <a href="#" onclick="return switchDateOptions();">Date Picker</a> <a href="#" onclick="return switchDateOptions();">Date Slider</a> </div> </div> 

CSS

<style>
.dropbtn {
    background-color: #3498DB;
    color: white;
    padding: 16px;
    font-size: 16px;
    border-radius: 25%;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #2980B9;
}

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

.dropdown-content {
    display: none;
    position: absolute;
    background-color: green; //change your color, use white or black
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

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

.dropdown a:hover {background-color: #ddd}

.show {display:block;}
</style>

HTML

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Change Data Filter</button>
  <div id="myDropdown" class="dropdown-content">
   <a href="#"><font style="color:red">Date Center</font></a>
    <a href="#">Date Slider</a>
  </div>
</div>

JS

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

// Close the dropdown 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');
      }
    }
  }
}
</script>

而且,如果您要更改(這兩個下拉菜單內容中的一個),只需使用html樣式。

更改它:

<a href="#home"><font style="color:red">Home</font></a>

我在代碼段中使用了您的代碼,看起來一切正常。 請參見下面的代碼段。

 function dropDownMenu() { document.getElementById("myDropdown").classList.toggle("show"); } // Close the dropdown 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: #3498DB; color: white; padding: 13px; font-size: 13px; border: none; position: absolute; right: -490px; top: 8px; background-color: #0076B1; border-radius: 25%; cursor: pointer; transition: 0.3s; } .dropbtn:hover, .dropbtn:focus { background-color: #2980B9; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: white; min-width: 160px; overflow: auto; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; right: -490px; top: 52px; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown a:hover {background-color: #ddd} .show {display:block;} 
 <div class="dropdown"> <button onclick="dropDownMenu()" class="dropbtn">Change Date Filter</button> <div id="myDropdown" class="dropdown-content"> <a href="#" onclick="return switchDateOptions();">Date Picker</a> <a href="#" onclick="return switchDateOptions();">Date Slider</a> </div> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM