gpt4 book ai didi

javascript - 将下拉菜单置于触发它的按钮下方

转载 作者:太空宇宙 更新时间:2023-11-04 01:15:12 24 4
gpt4 key购买 nike

我正在尝试创建一个下拉菜单,以便在一个简单的网站上更轻松地导航。我从 w3schools 借用了大量代码.免责声明是,我真的很不擅长样式和 js。

问题是当我添加一个额外的按钮时,菜单显示在第一个按钮下。

function myFunction(a) {

document.getElementById(a).classList.toggle("show");

}
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: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}


/* Dropdown button on hover & focus */

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


/* The container <div> - needed to position the dropdown content */

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


/* Dropdown Content (Hidden by Default) */

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


/* Links inside the dropdown */

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


/* Change color of dropdown links on hover */

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


/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */

.show {
display: block;
}
<div class="dropdown">
<button onclick="myFunction('myDropdown0')" class="dropbtn">Menu1</button>
<div id="myDropdown0" class="dropdown-content">
<a href="{% url 'App1:index' %}">choice1</a>
</div>
<button onclick="myFunction('myDropdown1')" class="dropbtn">Menu2</button>
<div id="myDropdown1" class="dropdown-content">
<a href="{% url 'App2:index' %}">Choice1</a>
<a href="{% url 'App2:page2' %}">Choice2</a>
</div>
</div>

感觉应该有一种简单的方法来编辑它以考虑按下按钮的位置,但我不确定如何。

最佳答案

您应该更新您的 window.onclick 函数和 HTML div 标签。一种解决方案是先关闭所有菜单,然后再打开菜单。

请尝试:

function myFunction(a) {
closeAllMenu();
document.getElementById(a).classList.toggle("show");
}

function closeAllMenu(){//before show menu, close all menu first
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
openDropdown.classList.remove('show');
}
}

window.onclick = function(event) { // click outside the menu, close menu
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: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}

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

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #ddd}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
	<div class="dropdown">
<button onclick="myFunction('myDropdown0')" class="dropbtn">Menu1</button>
<div id="myDropdown0" class="dropdown-content">
<a href="{% url 'App1:index' %}">choice1</a>
</div>
</div>
<div class="dropdown">
<button onclick="myFunction('myDropdown1')" class="dropbtn">Menu2</button>
<div id="myDropdown1" class="dropdown-content">
<a href="{% url 'App2:index' %}">Choice1</a>
<a href="{% url 'App2:page2' %}">Choice2</a>
</div>
</div>

关于javascript - 将下拉菜单置于触发它的按钮下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50306788/

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