gpt4 book ai didi

javascript - 单击菜单列表内部时,可单击的下拉菜单不应关闭

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

我正在制作可点击的下拉菜单

  1. 当用户点击绿色下拉菜单按钮时,菜单将打开
  2. 当用户再次单击绿色按钮时,菜单将关闭。
  3. 但是当下拉菜单打开并且用户在页面上任何位置的下拉菜单之外单击时,菜单将再次关闭。

我已经设置了所有这 3 个步骤并且工作正常,但我想要的是,当下拉菜单打开,并且用户在菜单区域(红色背景颜色区域)内单击时,菜单不应关闭。我只需要相同的 JavaScript 代码。即使您可以使用全新的 JAVASCRIPT 代码来完成此操作,也会很有帮助。

/* 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');
}
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

.dropbtn:hover,
.dropbtn:focus {
background-color: #3e8e41;
}

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

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
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: #f1f1f1
}

.show {
display: block;
}
<p>Click on the button to open the dropdown menu.</p>
<p>when click here menu will close</p>

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content" style="background:red;">
<p>when click here menu should not close</p>
<div id="test1">
<p>when click here menu should not close</p>
</div>
<div id="test2">
<div id="subtest1">
<p>when click here menu should not close</p>
</div>
<div id="subtest1">
<p>when click here menu should not close</p>
</div>
</div>
</div>
</div>
<p>when click here menu will close</p>

最佳答案

您只需在下拉列表中添加单击事件并停止传播以防止事件冒泡到窗口。因此,当在下拉列表中使用单击时,窗口对象永远不会捕获单击事件。

// Prevent event bubble up to window.
document.getElementById("myDropdown").addEventListener('click', function (event) {
event.stopPropagation();
});

// Inline event also working well, if you prefer this style.
<div id="myDropdown" onclick="event.stopPropagation()" class="dropdown-content" style="background:red;">

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

// Prevent event bubble up to window.
document.getElementById("myDropdown").addEventListener('click', function (event) {
event.stopPropagation();
});

// 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: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}

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

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
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: #f1f1f1}

.show {display:block;}
<p>Click on the button to open the dropdown menu.</p>
<p>when click here menu will close</p>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content" style="background:red;">

<p>when click here menu should not close</p>
<div id="test1">
<p>when click here menu should not close</p>
</div>
<div id="test2">
<div id="subtest1">
<p>when click here menu should not close</p>
</div>
<div id="subtest1">
<p>when click here menu should not close</p>
</div>
</div>
</div>
</div>
<p>when click here menu will close</p>

关于javascript - 单击菜单列表内部时,可单击的下拉菜单不应关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43425619/

25 4 0