gpt4 book ai didi

javascript - 如何让我的下拉菜单出现/对齐

转载 作者:行者123 更新时间:2023-11-28 06:01:45 26 4
gpt4 key购买 nike

所以我正在制作一个简单的网页,但我无法让我的下拉菜单正常工作。此时甚至不再出现,当它出现在水平线而不是垂直线时。

因为它不显示下拉列表。我错过了什么?我有不需要的额外东西吗?

function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(e) {
if (!e.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
for (var d = 0; d < dropdowns.length; d++) {
var openDropdown = dropdowns[d];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
   ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}

li {
float: left;
}

li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}

li.dropdown {
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

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

.dropdown-content a:hover {background-color: #f1f1f1}

.show {display:block;}
    <nav>
<ul>
<li><a href='front.html'>Home</a></li>
<li><a href='intro.html'>Intro</a></li>
<li><a href='how.html'>How it Works</a></li>
<li><a href='best.html'>Using it</a></li>
<li class ="dropdown">
<a href="javascript:void(0)" class="dropbtn"
onclick="myFunction()">Examples</a>
<ul class="dropdown-content" id="myDropdown">
<div class="dropdown-content" id="myDropdown">
<li><a href='saga.html'>SAGA</a></li>
<li><a href='portfolio.html'>Portfolio Optimization</a></li>
<li><a href='circuits.html'>Evolved Circuits</a></li>
</div>
</ul>
</li>
<li><a href='about.html'>References</a></li>
</ul>
</nav>

最佳答案

我刚刚删除了 ul 标签中的 div 并覆盖了下拉列表的 li 标签样式(删除了 float: left fot that li 标签)

function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(e) {
if (!e.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
for (var d = 0; d < dropdowns.length; d++) {
var openDropdown = dropdowns[d];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
   ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}

li {
float: left;
}
.dropdown-content li {
float: none !important;
}

li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}

li.dropdown {
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

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

.dropdown-content a:hover {background-color: #f1f1f1}

.show {display:block;}
    <nav>
<ul>
<li><a href='front.html'>Home</a></li>
<li><a href='intro.html'>Intro</a></li>
<li><a href='how.html'>How it Works</a></li>
<li><a href='best.html'>Using it</a></li>
<li class ="dropdown">
<a href="javascript:void(0)" class="dropbtn"
onclick="myFunction()">Examples</a>
<ul class="dropdown-content" id="myDropdown">
<li><a href='saga.html'>SAGA</a></li>
<li><a href='portfolio.html'>Portfolio Optimization</a></li>
<li><a href='circuits.html'>Evolved Circuits</a></li>
</div>
</ul>
</li>
<li><a href='about.html'>References</a></li>
</ul>
</nav>

关于javascript - 如何让我的下拉菜单出现/对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36704665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com