简体   繁体   English

切换隐藏/显示div可以在按钮上使用,但不能在链接上使用吗?

[英]Toggle hide/show div works on button, but not on link?

I want to be able to show and hide a certain div when clicking on a link, but my code works only if I use a button. 我希望能够在单击链接时显示和隐藏特定的div,但是我的代码仅在使用按钮时有效。 Why is this happening and how can i fix it? 为什么会发生这种情况,我该如何解决?

Here is my code: 这是我的代码:

 .popup { position: relative; display: inline-block; } .popup-content { display: none; position: absolute; width: 1000px; } .show { display:block; } 
 <div class="popup"> <button onclick="showPopup()" class="btnPopup thePopup">POPUP FROM BUTTON</button> <a class="linkStyle thePopup" onclick="javascript:showPopup()" href="#" return false;>POPUP FROM LINK</a> <div id="myPopup" class="popup-content" style="border:1px; border-style:solid; margin:10px; padding: 10px;width:200px;"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatibus quasi voluptas impedit. Culpa impedit? </div> </div> <script> /* When the user clicks on the button, toggle between hiding and showing the dropdown content */ function showPopup() { event.preventDefault(); document.getElementById("myPopup").classList.toggle("show"); return false; } // Close the dropdown if the user clicks outside of it window.onclick = function(event) { if (!event.target.matches('.thePopup')) { var dropdowns = document.getElementsByClassName("popup-content"); var i; for (i = 0; i < dropdowns.length; i++) { var openDropdown = dropdowns[i]; if (openDropdown.classList.contains('show')) { openDropdown.classList.remove('show'); } } } } </script> 

Also here it is a link for codepen: http://codepen.io/ZlatinaNyagolova/pen/LkOgzk 同样在这里,它是Codepen的链接: http ://codepen.io/ZlatinaNyagolova/pen/LkOgzk

UPDATE: 更新:

I added another class to the button and the link and used that class in the listener in my closing function. 我在按钮和链接中添加了另一个类,并在关闭函数中在侦听器中使用了该类。 This fixed it! 这样解决了! :) :)

The popup opens alright, but your function for closing the popup is causing the problems. 弹出窗口可以正常打开,但是关闭弹出窗口的功能引起了问题。

You attach a listener to the whole window's "onclick" function, and then proceed to close the popup - unless the event target matched ".btnPopup", ie your button was clicked. 您将侦听器附加到整个窗口的“ onclick”功能上,然后继续关闭弹出窗口-除非事件目标与“ .btnPopup”匹配,即单击了按钮。

So what's happening is: 所以发生了什么事:

  1. You click on the button, its "onclick" function shows the popup, the window's "onclick" function runs, but doesn't do anything because the event target matches '.btnPopup' 您单击按钮,其“ onclick”功能显示弹出窗口,窗口的“ onclick”功能运行,但由于事件目标与“ .btnPopup”匹配而没有执行任何操作
  2. You click on the link, its "onclick" function shows the popup, the window's "onclick" function runs, and as the event taget does not match .btnPopup it closes the popup immediately. 您单击链接,其“ onclick”功能将显示弹出窗口,窗口的“ onclick”功能将运行,并且由于事件标记不匹配.btnPopup,它将立即关闭弹出窗口。

You can fix it easily, by eg adding another class to both the button and the link and using that class in your window.onclick listener to check if "something" was clicked to open the popup: 您可以轻松修复它,例如,在按钮和链接中添加另一个类,然后在window.onclick侦听器中使用该类,以检查是否单击了“某物”以打开弹出窗口:

<button onclick="showPopup()" class="btnPopup Popup">POPUP FROM BUTTON</button>
<a class="linkStyle Popup" onclick="showPopup()" href="#">POPUP FROM LINK</a>
...
window.onclick = function(event) {
    if (!event.target.matches('.Popup')) {
        //Proceed with closing the popup

Only change Code like this : 只能像这样更改代码:

 window.onclick = function(event) {
        if (!event.target.matches('.btnPopup')&& !event.target.matches('.linkStyle')) {

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

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