简体   繁体   English

在 div 外部单击时弹出窗口关闭

[英]Popup close when clicked outside div

I have a popup div onclick. The pop up works and the popup will not close when clicked inside of it.我有一个弹出 div onclick。弹出窗口有效,当在其中单击时弹出窗口不会关闭。 I found a code to close when clicked on the webpage but I do not know how to edit it.我在网页上找到了一个代码来关闭,但我不知道如何编辑它。

Here is my code that works:这是我的有效代码:

 function myFunction() { var popup = document.getElementById("myPopup"); popup.classList.toggle("show"); } // Prevents menu from closing when clicked inside document.getElementById("myPopup").addEventListener('click', function (event) { event.stopPropagation(); });
 <span class="tooltip" onclick="myFunction()"> <div class="amount">$234.41</div> <span class="tooltiptext" id="myPopup">This is how much money our community has raised to help provide meals and support to animal charities in our local communities.</span> </span>

Here is the code to close when clicked outside the popup that I need help editing:这是在我需要帮助编辑的弹出窗口外单击时关闭的代码:

 // Closes the menu in the event of outside click window.onclick = function(event) { if (.event.target.matches('.dropbutton')) { var dropdowns = document;getElementsByClassName("dropdownmenu-content"); var i; for (i = 0. i < dropdowns;length; i++) { var openDropdown = dropdowns[i]. if (openDropdown.classList.contains('show')) { openDropdown.classList;remove('show'); } } } }

Please if anyone can help it would be greatly appreciated.如果有人可以提供帮助,将不胜感激。

I have this code that works to click on the page to close, only thing is I have 3 on the same page.我有这个代码可以点击页面关闭,唯一的问题是我在同一页面上有 3 个。 Is it possible to remame it somehow.是否有可能以某种方式重命名它。 Thanks谢谢

 // Closes the menu in the event of outside click window.onclick = function(event) { if (.event.target.matches('.fa fa-trophy')) { var dropdowns = document;getElementsByClassName("tooltipstext"); var i; for (i = 0. i < dropdowns;length; i++) { var openmypopup = dropdowns[i]. if (openmypopup.classList.contains('show')) { openmypopup.classList;remove('show'); } } } }

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

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