我无法打开多个模态

时间:2016-06-18 12:48:18

标签: javascript modal-dialog

我刚创建了一个模态,但我有多次查看。我不能,因为如果我单击Open Modal 1它可以工作,但不能在Open Modal 2

// 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";
  }
}
/* 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 {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}
/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}
.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<h2>Modal Example 1</h2>

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

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">×</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

<h2>Modal Example 2</h2>

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

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">×</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

2 个答案:

答案 0 :(得分:0)

查看jsfiddle上的代码,html上不能有两个具有相同ID的元素(并期望它们正常工作)。

这是您的原始代码(您有两个带myBtn ID的元素):

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

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

相反,您可以使所有按钮具有相同的类(例如modalbtn),并使用代码通过每个按钮并设置回调,如下所示:

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

只是为了澄清:document.getElementById()函数最多只返回一个值。

编辑2

如果您想对不同的按钮执行不同的操作,您需要为每个按钮进行回调(或使用存储在按钮内的数据,但我不会在此答案中进入此级别的详细信息)。这是一个例子:

var btns = document.getElementsByClassName("modalbtn");
for (var i = 0; i < btns.length; i++) {
  var btn = btns[i];
  btn.onclick = function() {
    var modal = document.getElementById('myModal-' + i);
    modal.style.display = "block";
  };
}

使用该代码,第一个按钮将显示标识为myModal-0的模式,第二个按钮将显示标识为myModal-1的模式,依此类推......

修改

这是原始答案(两个按钮打开相同的模态):

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

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

// When the user clicks the button, open the modal 
var btns = document.getElementsByClassName("modalbtn");
for (var i = 0; i < btns.length; i++) {
  var btn = btns[i];
  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";
  }
}
/* 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 {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}
/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}
.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<h2>Modal Example 1</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn1" class="modalbtn">Open Modal 1</button>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">×</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

<h2>Modal Example 2</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn2" class="modalbtn">Open Modal 2</button>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">×</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

答案 1 :(得分:0)

就像Sergio提到的,你在DOM中不能拥有相同的id。因此,您可以使用class作为标识符

希望下面的代码段有用。

如果您想在将来引用特定按钮

索引将非常有用

//openModal is class common to button
var _getButton = document.getElementsByClassName('openModal');
//Convert NodeList to array to use array methods
var btnArray=Array.prototype.slice.call(_getButton)
btnArray.forEach(function(item,index){ //forEach is Array method
(function(x){   //Creating closure to bind event with particular button
   item.addEventListener('click',function(){
    console.log(index)
       modal.style.display = "block";
   })
 }(index))

})

JSFIDDLE

相关问题