gpt4 book ai didi

javascript - 在这个 HTML 双下拉列表中,第一个下拉列表如何不被第二个按钮遮挡?

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

当显示第一个下拉菜单时,它的一部分被第二个下拉菜单中的按钮遮挡。这怎么能解决?这是从更大的代码中抽象出来的案例。在实际情况下,第二个下拉菜单实际上意外地弹出,而不是向下弹出(虽然似乎有足够的空间),所以遮蔽作用是相互的:第二个不仅在第一个打开时遮盖了一个选择,而且当它向上打开时,第一个遮盖了第二个的选择。

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

function myFunction2() {
document.getElementById("myDropdown2").classList.toggle("show");
}

function optclick1(option) {
var txt = "Mode: "+option;
document.getElementById("dropbut1").innerText = txt;
}

function optclick2(option) {
var txt = "Made: "+option;
document.getElementById("dropbut2").innerText = txt;
}

// Close the dropdown menu 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');
}
}
}
}
   /* Dropdown Button */
.dropbtn {
/*background-color: white;
color: black;
border: none;
*/
cursor: pointer;
}

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

/* 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: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* 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: #f1f1f1}

/* 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;}
 
<html>
<head>
<meta charset="utf-8"/>
<title>testdrop</title>
</head>
<body>
<table>
<tr>
<td>
<div class="dropdown">
<button id="dropbut1" onclick="myFunction1()" class="dropbtn">Mode: Link 1</button>
<div id="myDropdown1" class="dropdown-content">
<a href="#" onclick="optclick1('Link 1');">Link 1</a>
<a href="#" onclick="optclick1('Link 2');">Link 2</a>
<a href="#" onclick="optclick1('Link 3');">Link 3</a>
</div>
</div>
</td>
</tr>
<tr><td>&nbsp;</td></tr>
<tr>
<td>
<div class="dropdown">
<button id="dropbut2" onclick="myFunction2()" class="dropbtn">Made: Chain 1</button>
<div id="myDropdown2" class="dropdown-content">
<a href="#" onclick="optclick2('Chain 1');">Chain 1</a>
<a href="#" onclick="optclick2('Chain 2');">Chain 2</a>
<a href="#" onclick="optclick2('Chain 3');">Chain 3</a>
</div>
</div>
</td>
</tr>
</table>


</body>
</html>

最佳答案

#myDropdown1 {
z-index:1;
}

给下拉 div 一个 z 索引。

或者使用类来赋予样式

.dropdown-content {
z-index: 1;
}

关于javascript - 在这个 HTML 双下拉列表中,第一个下拉列表如何不被第二个按钮遮挡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41887333/

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