简体   繁体   English

单击时模态不会关闭

[英]Modal does not close, when clicked

So I have a menu, where models pop-up on click.所以我有一个菜单,点击时模型会弹出。 In each model, there's a close button, marked by an "x".在每个模型中,都有一个关闭按钮,标有“x”。 Only problem is, that the model does not close, when the "x" is clicked.唯一的问题是,当单击“x”时,模型不会关闭。

I have tried multiple ways of filing this seemingly simple problem.我尝试了多种方法来解决这个看似简单的问题。 But with no luck.但没有运气。
I have tried, using a z-index property, I have tried fiddling with the divs.我试过,使用 z-index 属性,我试过摆弄 div。 I have tried to link to bootstrap external links.我试图链接到引导程序外部链接。 I have tried different kind of "close" button.我尝试过不同类型的“关闭”按钮。 I have tried to modifying the javascript code.我试图修改 javascript 代码。 Still I have not arrived at the desired outcome.我仍然没有达到预期的结果。 Can anyone help ?任何人都可以帮忙吗?

Here's my code这是我的代码

 window.onload = function () { list = document.querySelectorAll(".Project"); for (let i = 0; i < list.length; i++) { list[i].addEventListener("click", function (e) { e.preventDefault(); let currentElement = e.target.parentNode; let modalId = currentElement.dataset.modal; let modal = document.getElementById(modalId); modal.style.display = "block"; }); } };
 .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); } .modal-content { background-color: #fefefe; margin: 15% auto; padding: 20px; border: 1px solid #888; width: 80%; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; }
 <li class="Project" data-modal="myModal_1"> <span id="myBtn_1"> Wer Baut Der Stadt </span> <span id="year"> 2019 </span> <div class="Describtion"> <p style="display:none;"> Identity and Font developed for the lecture series on architecture conducted by No Image in Berlin. </p> </div> <div id="myModal_1" class="modal"> <div class="modal-content"> <img src="Images/WER BAUT 2018/HVIDAktiv 20.png" width="1000px"> <span class="close">&times; </span> <p> Some text in the Modal..1 </p> </div> </div> </li>

It is very easy to use modal in Bootstrap.在 Bootstrap 中使用模态非常容易。 You just have to make sure that, as in the example below, jquery.js, popper.js and bootstrap.js are placed in order.您只需要确保,如下例所示,jquery.js、popper.js 和 bootstrap.js 是按顺序放置的。

 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <!-- Button trigger modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Launch demo modal </button> <!-- Modal --> <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

this is because you have a bad use of JS event delegation.这是因为您对 JS 事件委托的使用不当。

the corect way is tu use the matches method https://developer.mozilla.org/en-US/docs/Web/API/Element/matches正确的方法是使用匹配方法https://developer.mozilla.org/en-US/docs/Web/API/Element/matches

like this :像这样 :

 window.onload = function () { list = document.querySelectorAll(".Project"); document.querySelectorAll(".Project").forEach(LIelm=>{ LIelm.addEventListener('click', showHideModal) }) }; function showHideModal(e) { if (!e.target.parentNode.matches('.Project , .modal-content' )) return e.preventDefault(); let currentParent = e.target.parentNode; if (currentParent.matches('.Project') ){ document.getElementById( currentParent.dataset.modal ).style.display = "block"; } else { currentParent.parentNode.style.display = ""; } }
 .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); } .modal-content { background-color: #fefefe; margin: 15% auto; padding: 20px; border: 1px solid #888; width: 80%; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; }
 <li class="Project" data-modal="myModal_1"> <span id="myBtn_1"> Wer Baut Der Stadt ee</span> <span id="year"> 2019 </span> <div class="Describtion"> <p style="display:none;"> Identity and Font developed for the lecture series on architecture conducted by No Image in Berlin. </p> </div> <div id="myModal_1" class="modal"> <div class="modal-content"> <img src="https://picsum.photos/200/300" > <span class="close">&times; </span> <p>Some text in the Modal..1 </p> </div> </div> </li>

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

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