多个按钮和模式

Wal*_*987 2 html javascript events

我是 JS 的新手,虽然我买了一些书来开始正确学习,但我想知道是否有人可以提供建议?我的商店列出了许多产品。在商店页面上,我在每个项目的底部添加了一个按钮,并希望在单击按钮时执行操作。我已经应用了这段代码,它可以工作,但只能在第一个元素上 - 因为它基于 id:

HTML:

<button id="myBtn" class="openmodal">Open Modal</button>

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

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

</div>
Run Code Online (Sandbox Code Playgroud)

JS:

// Get the modal
 window.onload = function(){
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 on 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";
    }
}};
Run Code Online (Sandbox Code Playgroud)

我尝试过更改 JS 以适应这样的类名,但它不起作用:

// Get the modal
 window.onload = function(){
var modal = document.getElementsByClassName('modal');

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

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

// When the user clicks on 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";
    }
}};
Run Code Online (Sandbox Code Playgroud)

请问有人可以帮忙吗?

预先感谢克里斯

Mih*_*nut 5

您应该使用classes它,ids因为id它必须是唯一的。

然后,您必须使用方法为每个按钮绑定一个事件处理程序。clickonclick

<button class="openmodal myBtn">Open Modal</button>

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

  <!-- Modal content -->
  <div class="modal-content">
  <span class="close">&times;</span>
  <p>Some text in the Modal..</p>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

JS

var modals = document.getElementsByClassName('modal');
// Get the button that opens the modal
var btns = document.getElementsByClassName("openmodal");
var spans=document.getElementsByClassName("close");
for(let i=0;i<btns.length;i++){
   btns[i].onclick = function() {
      modals[i].style.display = "block";
   }
}
for(let i=0;i<spans.length;i++){
    spans[i].onclick = function() {
       modals[i].style.display = "none";
    }
 }
Run Code Online (Sandbox Code Playgroud)

这是工作解决方案。

<button class="openmodal myBtn">Open Modal</button>

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

  <!-- Modal content -->
  <div class="modal-content">
  <span class="close">&times;</span>
  <p>Some text in the Modal..</p>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)
var modals = document.getElementsByClassName('modal');
// Get the button that opens the modal
var btns = document.getElementsByClassName("openmodal");
var spans=document.getElementsByClassName("close");
for(let i=0;i<btns.length;i++){
   btns[i].onclick = function() {
      modals[i].style.display = "block";
   }
}
for(let i=0;i<spans.length;i++){
    spans[i].onclick = function() {
       modals[i].style.display = "none";
    }
 }
Run Code Online (Sandbox Code Playgroud)
var modals = document.getElementsByClassName('modal');
// Get the button that opens the modal
var btns = document.getElementsByClassName("openmodal");
var spans=document.getElementsByClassName("close");
for(let i=0;i<btns.length;i++){
    btns[i].onclick = function() {
        modals[i].style.display = "block";
    }
}
for(let i=0;i<spans.length;i++){
    spans[i].onclick = function() {
        modals[i].style.display = "none";
    }
}
Run Code Online (Sandbox Code Playgroud)