gpt4 book ai didi

javascript - 如何在CSS中为模态弹出创建多个按钮

转载 作者:行者123 更新时间:2023-11-30 14:41:44 24 4
gpt4 key购买 nike

我有这个来源的脚本 https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal2 .它工作正常,因此在单击按钮时弹出模式。现在我想添加更多的两个按钮,这样当点击任何一个按钮时,例如。按钮 1、按钮 2 和 3。它将弹出模式。我怎样才能做到这一点。

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[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";
}
}
body {
font-family: Arial, Helvetica, sans-serif;
}


/* The Modal (background) */

.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}


/* Modal Content */

.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}


/* Add Animation */

@-webkit-keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}

@keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}


/* The Close Button */

.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}

.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}

.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
height: 10%;
}

.modal-body {
padding: 2px 16px;
}

.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
postion: fixed;
height: 15%;
}
<h2>Animated Modal with Header and Footer</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">butoon 1</button>
<button id="myBtn">button 2</button>
<button id="myBtn">button 3</button>

<!-- The Modal -->
<div id="myModal" class="modal">

<!-- Modal content -->
<div style="width:100%;height:100vh;margin-top:-100px" class="modal-content">
<div class="modal-header">
<span class="close">&times;</span>
<h2>Modal Header</h2>
</div>
<div class="modal-body">
<p>chat messages goes here</p><br>
<p>chat messages goes here</p>
<br><br><br><br><br><br><br><br><br>
<p>chat messages goes here</p>
<p>chat messages goes here</p>
<br><br><br><br><br><br><br>
</div>
<div style="" class="modal-footer">
Chat: <input type="text" placeholder="Chat Message" class="form-control">
<div>Files Goes Here</div>
</div>
</div>

</div>

最佳答案

您只需要为这三个按钮添加事件监听器。所以,你只需要专注于这些部分:

var btn = document.getElementById("myBtn");

btn.onclick = function() {
modal.style.display = "block";
}

不能有多个具有相同 ID 的元素。这是犯罪行为。 一本护照不能多人持有。

因此,将它们转换为类:

<button class="myBtn">butoon 1</button>
<button class="myBtn">button 2</button>
<button class="myBtn">button 3</button>

现在,您需要遍历它们来为每个添加事件监听器:

var btns = document.getElementsByClassName("myBtn");

for (var i = 0; i < btns.length; i++) {
btns[i].onclick = function() {
modal.style.display = "block";
}
}

以上将起作用。这里给你一个片段:

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btns = document.getElementsByClassName("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
for (var i = 0; i < btns.length; i++) {
btns[i].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";
}
}
body {font-family: Segoe UI;}

/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}

@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}

/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}

.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}

.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
height:10%;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
postion:fixed;
height:15%;
}
<h2>Animated Modal with Header and Footer</h2>

<!-- Trigger/Open The Modal -->
<button class="myBtn">button 1</button>
<button class="myBtn">button 2</button>
<button class="myBtn">button 3</button>

<!-- The Modal -->
<div id="myModal" class="modal">

<!-- Modal content -->
<div style="width:100%;height:100vh;margin-top:-100px" class="modal-content">
<div class="modal-header">
<span class="close">&times;</span>
<h2>Modal Header</h2>
</div>
<div class="modal-body">
<p>chat messages goes here</p><br>
<p>chat messages goes here</p>
<br><br><br><br><br><br><br><br><br>
<p>chat messages goes here</p>
<p>chat messages goes here</p>
<br><br><br><br><br><br><br>
</div>
<div style="" class="modal-footer">
Chat: <input type="text" placeholder="Chat Message" class="form-control">
<div>Files Goes Here</div>
</div>
</div>

</div>

关于javascript - 如何在CSS中为模态弹出创建多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49530418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com