简体   繁体   English

一页上的多个模式,带有递增的CSS ID

[英]Multiple modals on one page with incremental CSS ids

I have more than eight modals on one page, each with a unique CSS id. 我在一页上有八个以上的模式,每个模式都有一个唯一的CSS ID。 I was using the following javascript when only using one modal, but it doesn't target each CSS id. 当仅使用一种模式时,我使用了以下javascript,但它没有针对每个CSS ID。 Without knowing much javascript, I'd like to know how to target each modal dynamically with a counter variable. 在不了解太多JavaScript的情况下,我想知道如何使用计数器变量动态地定位每个模式。

<button class="fsc-modal-button">Read Bio</button>
<div class="fsc-modal" id="fsc-modal-1">
    <div class="fsc-modal-content"><span class="fsc-modal-close">×</span</div>
</div>

<button class="fsc-modal-button">Read Bio</button>
<div class="fsc-modal" id="fsc-modal-2">
    <div class="fsc-modal-content"><span class="fsc-modal-close">×</span</div>
</div>


<script type="text/javascript">

var button = document.getElementsByClassName('fsc-modal-button')[0];
var modal = document.getElementsByClassName('fsc-modal')[0];
var span = document.getElementsByClassName('fsc-modal-close')[0];

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

span.onclick = function() {
    modal.style.display = "none";
}

window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>
function openModal(id) {
  var button = document.getElementsByClassName('fsc-modal-button')[0];
  var modal = document.getElementById('fsc-modal-' + id);
  var span = document.getElementsByClassName('fsc-modal-close')[0];

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

  span.onclick = function() {
   modal.style.display = "none";
  }

  window.onclick = function(event) {
  if (event.target == modal) {
     modal.style.display = "none";
  }
 }
}

you can call you function as openModal(1) and so on. 您可以调用openModal(1)等函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM