[英]Modals only showing last button content on multiple buttons
I'm new to coding and am hoping to create three buttons opening three different modals—-so far I've only been about to get the content of the last button to show up for all three (when I comment it out, the second button works for the first two buttons, and when that is commented out, the first works for the first button).我是编码新手,我希望创建三个按钮来打开三个不同的模式——到目前为止,我只是想让最后一个按钮的内容显示给所有三个(当我注释掉它时,第二个button 适用于前两个按钮,当注释掉时,第一个适用于第一个按钮)。 I've tried changing the divs to classes to no avail.
我尝试将 div 更改为类无济于事。 Any help would be appreciated!
任何帮助,将不胜感激!
HTML: HTML:
<!--First Button-->
<div class="btn">
<button id="btn-name">
<h1>NAME</h1>
</button>
</div>
<br>
<div class="btn">
<button id="btn-news">
<h1>NEWS</h1>
</button>
</div>
<br>
<div class="btn">
<button id="btn-cv">
<h1>CV</h1>
</button>
</div>
<!-- The Modal -->
<div id="modal1" class="modal1">
<!-- Modal content -->
<div class="modal1-content">
<span class="close1">×</span>
<h4>ONE</h4>
<h4>CONTACT</h4>
<p>. . .</p>
</div>
</div>
<!--Second Button-->
<!-- The Modal -->
<div id="modal2" class="modal2">
<!-- Modal content -->
<div class="modal2-content">
<span class="close2">×</span>
<h4>TWO</h4>
<h4>CURRENTLY</h4>
</div>
</div>
<!--Third Button-->
<!-- The Modal -->
<div id="modal3" class="modal3">
<!-- Modal content -->
<div class="modal3-content">
<span class="close3">×</span>
<h4>THREE</h4>
<h4>EDUCATION</h4>
</div>
</div>
JS: JS:
<script>
// Get the modal
var modal = document.getElementById("modal1");
// Get the button that opens the modal
var btn = document.getElementById("btn-name");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close1")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.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";
}
}
</script>
<!--Second Button-->
<script>
// Get the modal
var modal = document.getElementById("modal2");
// Get the button that opens the modal
var btn = document.getElementById("btn-news");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close2")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.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";
}
}
</script>
<!--Third Button-->
<script>
// Get the modal
var modal = document.getElementById("modal3");
// Get the button that opens the modal
var btn = document.getElementById("btn-cv");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close3")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.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";
}
}
</script>
I'm guessing there is a lot of redundancy in this code.我猜这段代码有很多冗余。 .
. .
. Optimistically,
乐观地说,
JTE捷特
The problem with your code is you declared the var modal
and var btn
with the same name for your 3 modal and button.您的代码的问题是您为 3 modal 和 button 声明了具有相同名称的
var modal
和var btn
。 Using multiple <script></script>
does not mean they are separated from other codes.使用多个
<script></script>
并不意味着它们与其他代码分开。
Try declared with with different name.尝试使用不同的名称声明。
Sample Code:示例代码:
<script>
// Get the modal
var modal1 = document.getElementById("modal1");
// Get the button that opens the modal
var btn1 = document.getElementById("btn-name");
// Get the <span> element that closes the modal
var span1 = document.getElementsByClassName("close1")[0];
// When the user clicks the button, open the modal
btn1.onclick = function() {
modal1.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span1.onclick = function() {
modal1.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal1) {
modal1.style.display = "none";
}
}
</script>
<!--Second Button-->
<script>
// Get the modal
var modal2 = document.getElementById("modal2");
// Get the button that opens the modal
var btn2 = document.getElementById("btn-news");
// Get the <span> element that closes the modal
var span2 = document.getElementsByClassName("close2")[0];
// When the user clicks the button, open the modal
btn2.onclick = function() {
modal2.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span2.onclick = function() {
modal2.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal2) {
modal2.style.display = "none";
}
}
</script>
<!--Third Button-->
<script>
// Get the modal
var modal3 = document.getElementById("modal3");
// Get the button that opens the modal
var btn3 = document.getElementById("btn-cv");
// Get the <span> element that closes the modal
var span3 = document.getElementsByClassName("close3")[0];
// When the user clicks the button, open the modal
btn3.onclick = function() {
modal3.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span3.onclick = function() {
modal3.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal3) {
modal3.style.display = "none";
}
}
</script>
Also if you have this kind of Code.另外,如果您有这种代码。 Just use one
<script></script>
only.只使用一个
<script></script>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.