这是我之前的主题中的后续问题 Creating Multiple Modals on a Single Page
在以下建议中,它们都完美地工作,我假设我可以在与按钮元素相对的图像上进行模态工作,但似乎并非如此。我可以完全打开模态然而×不会触发模态的关闭,我无法弄清楚为什么。我禁用了附加到我页面的所有CSS,假设有些东西干扰了它,但我对相同的结果很有趣,所以我有理由相信问题在javascript中?有人会有任何想法如何解决它?感谢。
// Get the modal
var modal = document.getElementsByClassName('modal');
// Get the button that opens the modal
var btn = document.getElementsByClassName("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close");
// When the user clicks the button, open the modal
btn[0].onclick = function() {
modal[0].style.display = "block";
}
btn[1].onclick = function() {
modal[1].style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span[0].onclick = function() {
modal[0].style.display = "none";
}
span[1].onclick = function() {
modal[1].style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
<div class="four columns">
<div class="myBtn">
<figure class="effect-zoe">
<img src="images/sample-1.jpg" alt="img25"/>
<figcaption>
<h2>Kinetic Kids Rebrand</h2>
<p class="description">Zoe never had the patience of her sisters. She deliberately punched the bear in his face.</p>
</figcaption>
</figure>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Project 1</h2>
</div>
<div class="modal-body">
<img src="images/sample-1.jpg" alt="img25"/>
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
</div>
</div>
</div>
</div>
<div class="four columns">
<div class="myBtn">
<figure class="effect-zoe">
<img src="images/sample-1.jpg" alt="img25"/>
<figcaption>
<h2>Cyber Block App</h2>
<p class="description">Zoe never had the patience of her sisters. She deliberately punched the bear in his face.</p>
</figcaption>
</figure>
<!-- The Modal -->
<div id="myModal2" class="modal">
<!-- Modal content -->
<div class="modal2-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Project 2</h2>
</div>
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
你的问题是关闭模态的跨度是在&#34;按钮&#34;的div内部。打开模态。
当点击事件在跨度上触发时,它也会在父div上触发。因此,模态会立即隐藏,然后重新显示。
要解决此问题,请确保将按钮的div与模式的div分开:
// Get the modal
var modal = document.getElementsByClassName('modal');
// Get the button that opens the modal
var btn = document.getElementsByClassName("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close");
// When the user clicks the button, open the modal
btn[0].onclick = function() {
modal[0].style.display = "block";
}
btn[1].onclick = function() {
modal[1].style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span[0].onclick = function() {
modal[0].style.display = "none";
}
span[1].onclick = function() {
modal[1].style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
&#13;
<div class="four columns">
<div class="myBtn">
<figure class="effect-zoe">
<img src="images/sample-1.jpg" alt="img25" />
<figcaption>
<h2>Kinetic Kids Rebrand</h2>
<p class="description">Zoe never had the patience of her sisters. She deliberately punched the bear in his face.</p>
</figcaption>
</figure>
</div>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Project 1</h2>
</div>
<div class="modal-body">
<img src="images/sample-1.jpg" alt="img25" />
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
</div>
</div>
</div>
<div class="four columns">
<div class="myBtn">
<figure class="effect-zoe">
<img src="images/sample-1.jpg" alt="img25" />
<figcaption>
<h2>Cyber Block App</h2>
<p class="description">Zoe never had the patience of her sisters. She deliberately punched the bear in his face.</p>
</figcaption>
</figure>
</div>
<!-- The Modal -->
<div id="myModal2" class="modal">
<!-- Modal content -->
<div class="modal2-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Project 2</h2>
</div>
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
</div>
</div>
</div>
&#13;