HTML下拉菜单文本不会出现

时间:2018-04-03 18:32:11

标签: javascript html css

您好我对html下拉菜单有疑问。

我只是按照w3学校的指示创建了下拉菜单。 (https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown

一切正常,但当我点击菜单按钮时,我无法看到如下的菜单列表。我能解决这个问题吗?

no filter menu

gawk
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;}

2 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

我在代码段中使用您的代码,看起来它工作正常。请参阅下面的代码段。

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>