简体   繁体   English

当鼠标在外部单击时,Clickable下拉菜单不会关闭

[英]Clickable dropdown menu does not close when the mouse click outside

(Please note if this question is not appropriate to post here, please remove it. Thanks) (请注意,如果此问题不适合在此处发布,请删除它。谢谢)

I am learning jsp and try to make a clickable dropdown menu in the jsp file. 我正在学习jsp,并尝试在jsp文件中创建一个可单击的下拉菜单。 I read some posts about dropdown menu using servlet or ajax( display data from servlet to drop down menu jsp , JSP populate dropdown list from database on button click and Populating cascading dropdown lists in JSP/Servlet ). 我阅读了一些有关使用servlet或ajax的下拉菜单的文章( 显示来自servlet的数据以下拉菜单jsp单击按钮从JSP填充数据库中的下拉列表,然后 在JSP / Servlet中 填充 级联下拉列表 )。

However I don't know too much about servlet or ajax so I focus to try to make a simple, clickable dropdown menu. 但是,我对servlet或ajax不太了解,因此我重点尝试制作一个简单的,可单击的下拉菜单。

I find this post is useful, its solution is what I am trying to do. 我发现这篇文章很有用,它的解决方案是我想要做的。 So I try to apply the solution from that post in the jsp file. 因此,我尝试从jsp文件中的帖子中应用解决方案。 The file name is dropdownmenu.jsp. 文件名是dropdownmenu.jsp。 Here is the code of the file (css part has been removed in order to minimize the content). 这是文件的代码(为了最小化内容,已删除了css部分)。

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

//for 2nd dropdown
function myFunction2() {
document.getElementById("myDropdown2").classList.toggle("show2");
}

//for 3rd dropdown

function myFunction3() {
document.getElementById("myDropdown3").classList.toggle("show3");
}

// 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');
    }
}
}

if (!event.target.matches('.dropbtn2')) {

var dropdowns = document.getElementsByClassName("dropdown-content2");
var i;
for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];
    if (openDropdown.classList.contains('show2')) {
        openDropdown.classList.remove('show2');
    }
}
}

 if (!event.target.matches('.dropbtn3')) {

var dropdowns = document.getElementsByClassName("dropdown-content3");
var i;
for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];
    if (openDropdown.classList.contains('show3')) {
        openDropdown.classList.remove('show3');
    }
}
}
}

</script>

<h2>Clickable Dropdown</h2>
<p>Click on the button to open the dropdown menu.</p>

1st dropdown
<div class="dropdown">
<button onclick="javascript:myFunction();" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>

2nd dropdown
<div class="dropdown2">
<button onclick="javascript:myFunction2();" class="dropbtn2">Dropdown2</button>
<div id="myDropdown2" class="dropdown-content2">
<a href="#home">Home2</a>
<a href="#about">About2</a>
<a href="#contact">Contact2</a>
</div>
</div>

3rd dropdown
<div class="dropdown3">
<button onclick="javascript:myFunction3();" class="dropbtn3">Dropdown3</button>
<div id="myDropdown3" class="dropdown-content3">
<a href="#home">Home3</a>
<a href="#about">About3</a>
<a href="#contact">Contact3</a>
</div>
</div>

I run it, when I click the button, it will open the dropdown menu, however when I click outside of the button, it does not close the menu. 我运行它,当我单击按钮时,它将打开下拉菜单,但是当我单击按钮之外时,它不会关闭菜单。 If I click the other button, it opens the dropdown menu and the previous menu still open. 如果单击另一个按钮,它将打开下拉菜单,而上一个菜单仍处于打开状态。 If I click the last button and it opens the menu but the previous two dropdown menus do not close. 如果单击最后一个按钮,它将打开菜单,但是前两个下拉菜单不会关闭。 When I click outside of those three buttons, three dropdown menus do not close. 当我在这三个按钮之外单击时,三个下拉菜单不会关闭。 I notice if I need to close the dropdown menu, I have to click the button to close. 我注意到是否需要关闭下拉菜单,我必须单击按钮以关闭。

I feel strange about the result because in that post , the accepted answer works fine and I just use that code without edit in the jsp file. 我对结果感到奇怪,因为在那篇文章中 ,可接受的答案很好用,而我只是使用该代码而无需在jsp文件中进行编辑。 But the dropdown menu do not close when click outside of the button. 但是,在按钮外部单击时,下拉菜单不会关闭。

I follow this post and this post to call a function from a button click. 我按照这篇文章和这篇文章通过单击按钮调用函数。 So some code has been changed 所以一些代码已经改变

<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<button onclick="myFunction2()" class="dropbtn2">Dropdown2</button>
<button onclick="myFunction3()" class="dropbtn3">Dropdown3</button>

changed to 变成

<button onclick="javascript:myFunction()" class="dropbtn">Dropdown</button>
<button onclick="javascript:myFunction2()" class="dropbtn2">Dropdown2</button>
<button onclick="javascript:myFunction3()" class="dropbtn3">Dropdown3</button>

However the result is the same, not sure which part I made the mistake. 但是结果是一样的,不确定我犯了哪个错误。

So I would wonder to know how to close the dropdown menu when the mouse click outside the button? 因此,我想知道当鼠标在按钮外部单击时如何关闭下拉菜单吗? Thanks a lot. 非常感谢。

Javascript is client side programming language and jsp is server side programming language. Javascript是客户端编程语言,而jsp是服务器端编程语言。

So window.onclick will not work in jsp file, you may find the difference between javascript and jsp in this post 所以window.onclick不会在JSP文件时,您可能会发现在这个JavaScript和JSP的区别

To make a clickable dropdown menu in a jsp file, I suggest to use jquery and you may have a look at this website . 要在jsp文件中创建可点击的下拉菜单,建议您使用jquery,您可以查看此网站

If you don't know about jquery, jQuery Learning Center and jQuery Tutorial would be a good resource to understand how jquery works. 如果您不了解jquery,则jQuery Learning CenterjQuery Tutorial将是了解jquery如何工作的好资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM