首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的JavaScript弹出模式只适用于第一个项目?

我的JavaScript弹出模式只适用于第一个项目?
EN

Stack Overflow用户
提问于 2018-08-29 09:12:41
回答 1查看 166关注 0票数 0

你好,我想弹出一个模式,一旦用户点击一个div出现,我有一个脚本工作,但它将只在第一个div而不是所有的工作。我已经链接了下面的代码。你觉得我哪里错了?

代码语言:javascript
复制
    <!-- Trigger/Open The Modal -->
<div class="job-wrap">
  <button id="myBtn">
    <div class="job-box">
      <div class="text-box">
        <p class="position-type">Part Time</p>
        <p class="job-role">Graphic Designer</p>
        <p class="company-name">Deans School Supply</p>
      </div>
      <div class="time-box">
        <p>9 Days ago</p>
      </div>
    </div>
  </button>
</div>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p class="job-type">Full Time</p>
  </div>

</div>

<script>
// 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 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";
}
}
</script>
EN

回答 1

Stack Overflow用户

发布于 2018-08-29 09:22:20

首先,id属性在页面上应该是唯一的,不要重复它!(我假设这就是你正在做的,因为你想要多个按钮来打开模式)。其次,您只需要将第一个找到的iddocument.getElementById函数进行匹配。您应该使用class属性和document.getElementsByClassName函数。

代码语言:javascript
复制
<!-- Trigger/Open The Modal -->
<div class="job-wrap">
  <button class="myBtn">
    <div class="job-box">
      <div class="text-box">
        <p class="position-type">Part Time</p>
        <p class="job-role">Graphic Designer</p>
        <p class="company-name">Deans School Supply</p>
      </div>
      <div class="time-box">
        <p>9 Days ago</p>
      </div>
    </div>
  </button>
</div>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p class="job-type">Full Time</p>
  </div>

</div>

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

// Get the button that opens the modal
var btns = document.etElementsByClassName("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
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";
}
}
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52068172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档