开发者问题收集

并非所有按钮都打开模式

2020-07-15
41

我正在创建一个模式,当用户点击我的三个按钮中的任何一个时,它目前只对第一个按钮起作用,但是当我关闭模式并点击另一个按钮时,模式不会打开,我不知道为什么。我怎样才能让我的模式打开,无论我在关闭模式后点击了哪个按钮?

这是我的 html:

<div class="modal" id="modal">
                        <button id="close">X</button>
                        <h5>Modal</h5>
                      
                      </div>
                    <div class="column-container">
                        <div class="first-column">
                                <h3>Affiliate Manager</h3>
                                <br>
                                <p>
                                    If you are passionate about Affiliate marketing and searching for your next career achievement, please do not hesitate to contact us.
                                </p>
                            </div>
                                <div class="second-column">
                                   <button class="apply-btn jsModalTrigger open">APPLY NOW</button>
                              </div>
                              <div class="first-column">
                                <h3>Affiliate Manager</h3>
                                <br>
                                <p>
                                    If you are passionate about Affiliate marketing and searching for your next career achievement, please do not hesitate to contact us.
                                </p>
                            </div>
                                <div class="second-column">
                                   <button class="apply-btn jsModalTrigger open">APPLY NOW</button>
                              </div>
                              <div class="first-column">
                                <h3>Affiliate Manager</h3>
                                <br>
                                <p>
                                    If you are passionate about Affiliate marketing and searching for your next career achievement, please do not hesitate to contact us.
                                </p>
                            </div>
                                <div class="second-column">
                                   <button class="apply-btn jsModalTrigger open">APPLY NOW</button>
                              </div>

我的 css:

.modal {
    width: 300px;
    height: 150px;
    margin: auto auto;
    background: #34495e;
    display: none;
  }
  
  .modal button {
    right: 0;
    top: 0;
  }

和我的 javascript:

var btnOpen = document.querySelector(".open");
var btnClose = document.getElementById("close");
var modal = document.getElementById("modal");

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

btnClose.onclick = function(){
  modal.style.display = "none";
}
1个回答

您只是获取 dom 中 className 为 open 的第一个元素。将此:

var btnOpen = document.querySelector(".open"); 

更改为以下内容:

var btnsOpen = document.querySelectorAll(".open");

这将返回一个元素数组。

然后您必须遍历该数组并添加事件

btnsOpen.forEach(btn=>{
      btn.onclick = function(){
      modal.style.display = "block";
    }
 })
var btnsOpen = document.querySelectorAll(".open");
var btnClose = document.getElementById("close");
var modal = document.getElementById("modal");

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


btnClose.onclick = function(){
  modal.style.display = "none";
}
<div class="modal" id="modal">
                        <button id="close">X</button>
                        <h5>Modal</h5>
                      
                      </div>
                    <div class="column-container">
                        <div class="first-column">
                                <h3>Affiliate Manager</h3>
                                <br>
                                <p>
                                    If you are passionate about Affiliate marketing and searching for your next career achievement, please do not hesitate to contact us.
                                </p>
                            </div>
                                <div class="second-column">
                                   <button class="apply-btn jsModalTrigger open">APPLY NOW</button>
                              </div>
                              <div class="first-column">
                                <h3>Affiliate Manager</h3>
                                <br>
                                <p>
                                    If you are passionate about Affiliate marketing and searching for your next career achievement, please do not hesitate to contact us.
                                </p>
                            </div>
                                <div class="second-column">
                                   <button class="apply-btn jsModalTrigger open">APPLY NOW</button>
                              </div>
                              <div class="first-column">
                                <h3>Affiliate Manager</h3>
                                <br>
                                <p>
                                    If you are passionate about Affiliate marketing and searching for your next career achievement, please do not hesitate to contact us.
                                </p>
                            </div>
                                <div class="second-column">
                                   <button class="apply-btn jsModalTrigger open">APPLY NOW</button>
                              </div>
Arm144
2020-07-15