我已經採取了一個工作腳本,並將其調整爲允許多個獨立模態,並且我不再能夠關閉任何模態。我收到錯誤:莫代爾'關閉'不工作
Cannot set property 'display' of undefined at HTMLSpanElement.span.onclick
window.onclick或document.onclick函數都不起作用。 Here's the 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";
}
}
可能重複[隱藏模式在Twitter Bootstrap點擊時](https://stackoverflow.com/questions/17163476/hide-modal-in-twitter-bootstrap-when-clicked) –
@ C.Lightfoot這是Bootstrap模式,這是一個自定義的。 – yuriy636