gpt4 book ai didi

javascript - 如何防止下拉菜单在用户单击时关闭?

转载 作者:行者123 更新时间:2023-11-28 17:25:39 24 4
gpt4 key购买 nike

我关注了一个 tutorial在 W3Schools 网站上关于从按钮制作下拉菜单。一切正常,除了当用户在下拉菜单中单击时,菜单本身会消失。

我想制作一个登录按钮,用户点击该按钮,它会显示一个下拉菜单,他/她可以在其中输入他/她的用户名和密码,然后点击登录。

我想保留这个功能,如果用户点击下拉菜单的任何其他区域,它会自动关闭下拉菜单。

我的问题是,当用户单击下拉菜单内部 的任何区域时,下拉菜单会消失。仅使用 JavaScript 在下拉菜单中单击时,如何防止它消失?使用 jQuery 怎么样(我还不太熟悉 jQuery)

/* 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 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: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
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);
}

/* Contents inside the dropdown */
.dropdown-content input, .dropdown-content button {
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;}
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">LOG IN</button>
<div id="myDropdown" class="dropdown-content">
<input type="text" name="username" placeholder="Enter username"/>
<input type="text" name="password" placeholder="Enter password"/>
<button type="submit" name="submit">Submit</button>
</div>
</div>

最佳答案

在 window.onClick 处理程序中,我们添加了一个条件来排除“下拉内容”及其子项。

/* 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 menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
// [BEGIN][Here is the condition]
if (event.target.matches('.dropdown-content') || event.target.matches('.dropdown-content *') ) {
event.stopPropagation();
return;
}
// [END][Here is the condition]
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: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
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);
}

/* Contents inside the dropdown */
.dropdown-content input, .dropdown-content button {
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;}
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">LOG IN</button>
<div id="myDropdown" class="dropdown-content">
<input type="text" name="username" placeholder="Enter username"/>
<input type="text" name="password" placeholder="Enter password"/>
<button type="submit" name="submit">Submit</button>
</div>
</div>

关于javascript - 如何防止下拉菜单在用户单击时关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253660/

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