[英]Modal 'close' not working
I've taken a working script and adjusted it to allow for multiple independent modals and I am no longer able to close any of the modals. 我已经使用了一个工作脚本并对其进行了调整,以允许使用多个独立的模态,但我不再能够关闭任何模态。 I receive the error:
我收到错误:
Cannot set property 'display' of undefined at HTMLSpanElement.span.onclick
无法在HTMLSpanElement.span.onclick中设置未定义的属性“显示”
Neither the window.onclick or document.onclick functions are working. window.onclick或document.onclick函数均无效。 Here's the jsfiddle
这是jsfiddle
<div class="contentSection">
<a class="popupBtn" data-modal="modal-window-one">Open Me!</a>
</div>
<div id="modal-window-one" class="popupModal modal">
<span class="close">×</span>
<div class="modalContent">
<p>Here's some content!</p>
</div>
</div>
<div class="contentSection">
<a class="popupBtn" data-modal="modal-window-two">...Or Open Me!</a>
</div>
<div id="modal-window-two" class="popupModal modal">
<span class="close">×</span>
<div class="modalContent">
<p>Here's some different content!</p>
</div>
</div>
var modal = document.getElementsByClassName('popupModal');
var btn = document.getElementsByClassName("popupBtn");
var span = document.getElementsByClassName("close")[0];
for (var i = 0; i < btn.length; i++) {
var thisBtn = btn[i];
thisBtn.addEventListener("click", function(){
var modal = document.getElementById(this.dataset.modal);
modal.style.display = "block";
}, false);
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
modal
is an HTMLCollection
(array like object), as you have class popupModal
for more than one element. modal
是一个HTMLCollection
(类似于对象的数组),因为您有多个元素的popupModal
类。 So you need to iterate through the array and set display = none
for all. 因此,您需要遍历数组并为所有设置
display = none
。
Also, you have assigned click
for the first close
element only, so you need to iterate through the span
elements and attach click
for both span elements to close both modal windows. 另外,您只为第一个
close
元素分配了click
,因此您需要遍历span
元素并为两个span元素附加click
以关闭两个模式窗口。
Or, you can just iterate through the modal
array and attach event listener to close the modal if event.target
is span
. 或者,如果
event.target
为span
,则可以遍历modal
数组并附加事件侦听器以关闭模式。
var modal = document.getElementsByClassName('popupModal'); var btn = document.getElementsByClassName("popupBtn"); var span = document.getElementsByClassName("close"); for (var i = 0; i < btn.length; i++) { var thisBtn = btn[i]; thisBtn.addEventListener("click", function() { var modal = document.getElementById(this.dataset.modal); modal.style.display = "block"; }, false); } for (var i = 0; i < modal.length; i++) { modal[i].addEventListener("click", function(e) { if(e.target.className === 'close') { this.style.display = 'none'; } }, false); }
.popupBtn { cursor: pointer; } .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.4); cursor: pointer; } .modalContent { background-color: #fefefe; margin: 15% auto; padding: 20px; border-top: 8px solid #81c8e8; border-radius: 4px; width: 50%; box-shadow: 0px 2px 8px #222; cursor: default; font-size: .9em; } .close { color: #ccc; float: right; font-size: 25pt; font-weight: bold; transition: ease-in-out .5s; padding: 10px 30px; background-color: #2b3d52; } .close:hover, .close:focus { color: #fff; text-decoration: none; cursor: pointer; }
<div class="contentSection"> <a class="popupBtn" data-modal="modal-window-one">Open Me!</a> </div> <div id="modal-window-one" class="popupModal modal"> <span class="close">×</span> <div class="modalContent"> <p> Here's some content! </p> </div> </div> <div class="contentSection"> <a class="popupBtn" data-modal="modal-window-two">...Or Open Me!</a> </div> <div id="modal-window-two" class="popupModal modal"> <span class="close">×</span> <div class="modalContent"> <p> Here's some different content! </p> </div> </div>
I saw a couple of things that were not right in your code: 我看到了一些不正确的代码:
1) you were using twice var modal
1)您使用了两次
var modal
2) you subscribe to onclick
of only one <span></span>
2)您只订阅一个
<span></span>
onclick
3) var model = document.getElementsByClassName('popupModal');
3)
var model = document.getElementsByClassName('popupModal');
will actually return a HTMLCollection and this code: 实际上将返回HTMLCollection和以下代码:
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
will never work( you need to think to refactor it ) 将永远无法工作( 您需要考虑对其进行重构 )
So you need to capture the click
of both spans and it should work 因此,您需要捕获两个跨度的
click
,它应该可以工作
var modals = document.getElementsByClassName('popupModal'); var btns = document.getElementsByClassName("popupBtn"); var spans = document.getElementsByClassName("close"); for (var i = 0; i < btns.length; i++) { var thisBtn = btns[i]; thisBtn.addEventListener("click", function() { var m = document.getElementById(this.dataset.modal); m.style.display = "block"; }, false); } for (var i = 0; i < spans.length; i++) { var thisSpan = spans[i]; thisSpan.addEventListener("click", function() { this.parentNode.style.display = "none"; }, false); }
.popupBtn { cursor: pointer; } .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.4); cursor: pointer; } .modalContent { background-color: #fefefe; margin: 15% auto; padding: 20px; border-top: 8px solid #81c8e8; border-radius: 4px; width: 50%; box-shadow: 0px 2px 8px #222; cursor: default; font-size: .9em; } .close { color: #ccc; float: right; font-size: 25pt; font-weight: bold; transition: ease-in-out .5s; padding: 10px 30px; background-color: #2b3d52; } .close:hover, .close:focus { color: #fff; text-decoration: none; cursor: pointer; }
<div class="contentSection"> <a class="popupBtn" data-modal="modal-window-one">Open Me!</a> </div> <div id="modal-window-one" class="popupModal modal"> <span class="close">×</span> <div class="modalContent"> <p> Here's some content! </p> </div> </div> <div class="contentSection"> <a class="popupBtn" data-modal="modal-window-two">...Or Open Me!</a> </div> <div id="modal-window-two" class="popupModal modal"> <span class="close">×</span> <div class="modalContent"> <p> Here's some different content! </p> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.