【问题标题】:how to create multiple button for modal popup in css如何在css中为模式弹出窗口创建多个按钮
【发布时间】:2018-03-28 09:09:32
【问题描述】:

我有来自这个来源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>

【问题讨论】:

  • 使用一个类和 getElementsByClassName 然后循环遍历你的按钮 - id 应该是唯一的,因此 getElementById 只会找到一个项目

标签: javascript html css


【解决方案1】:

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

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";
  }
}

上述方法将起作用。在这里给你一个sn-p:

// 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>

【讨论】:

  • 非常感谢,我给你买瓶啤酒。
  • @chinazaike 哈哈!你人真好。我是一个滴酒不沾的人。我会得到一个很好的设计灵感。 ;)
【解决方案2】:

var btns = document.getElementsByClassName('myBtn')
var mdl = document.getElementById('myModal');
var clo = document.getElementsByClassName("close")
for(btn of btns){
btn.addEventListener('click',function(){
mdl.style.display = "block";
})
clo[0].addEventListener('click',function(){
mdl.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>

【讨论】:

    猜你喜欢
    • 2022-10-01
    • 2010-10-23
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多