当放置在具有 show/hide 函数的 div 中时,模态不显示

modal doesn't show when placed inside a div that has a show/hide function

我有一个简单的 multi-image 模式设置,效果很好。但是,我尝试将其集成到的页面设置如下:我有一个 header,左侧有一组 link,[=33= 旁边有一个主要部分]s。单击 link 时,它会通过 hide/show 函数更改主要部分的内容。以这种方式,所有站点都在一页上。问题是,当我将模态框放置在选择的隐藏 div 内时,主要内容更改时图像会显示,但单击图像时模态框不会打开。这是主要 div 和隐藏 div 模态所在的代码(用户需要单击显示“fall bill”的图像所在的位置以打开 div 模态所在的位置。您会看到我将一些图像作为持有人放置。图像在悬停时会按照预期消失。我无法弄清楚模态在这种情况下不会出现的任何原因,但如果它可以工作不在可以隐藏的 div 中。

这是代码:

var modal = document.getElementById('myModal');
var images = document.getElementsByClassName('img');
var modalImg = document.getElementById("img01");

for (var i = 0; i < images.length; i++) {
  var img = images[i];
  img.onclick = function(evt) {
    console.log(evt);
    modal.style.display = "block";
    modalImg.src = this.src;
  }
}

var span = document.getElementsByClassName("close")[0];

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


function show(param_div_id) {
  document.getElementById('main').innerHTML = document.getElementById(param_div_id).innerHTML;
}
.img {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

.img:hover {
  opacity: 0.8;
}

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 10;
  /* 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.9);
  /* Black w/ opacity */
}

.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

.modal-content,
#caption {
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

@media only screen and (max-width: 700px) {
  .modal-content {
    width: 100%;
  }
}

#main {
  background-image: url('bkgd.jpg');
  display: block;
  width: 690px;
  font-family: "Book Antiqua";
  color: #004B97;
  font-size: 16pt;
  padding: 2%;
  margin: 5px;
  border-radius: 15px;
  border: medium #9999FF solid;
}

.mainContents {
  display: flex;
  flex-wrap: wrap;
  margin: 0 auto;
  position: relative;
  width: 690px;
  flex-wrap: wrap;
  justify-content: center;
}

#home,
#bills,
#munTax,
#schoolTax,
#interimTax,
#taxCert,
#dupsReceipts,
#directions,
#FAQ {
  display: none;
  width: 300px;
  font-family: "Book Antiqua";
  color: #004B97;
}

.head {
  width: 100%;
  text-align: center;
  font-family: inherit;
  font-size: 16pt;
  color: #004B97;
  padding-bottom: 10px;
}

.head2 {
  position: relative;
  width: 100%;
  text-align: center;
  font-family: inherit;
  font-size: 14pt;
  color: #835BF9;
  padding-bottom: 15px;
}

.lower {
  width: 100%;
  display: flex;
  margin: 0 auto auto 10;
  flex-wrap: wrap;
  justify-content: space-around;
  padding: 10px;
  align-items: center;
}

.pay {
  text-align: center;
  background-image: url('background.jpg');
  color: #0D095E;
  font-size: 14pt;
  border: medium #9999FF solid;
  border-radius: 15px;
  padding: 5px;
  margin: 5px;
}
<div id="main">
  <div class="mainContents">
    <div class="head">Outside Receiving Hours:<br/> Please use secure drop box outside building
    </div>
    <div class="head2">
      <strong>2022 Millage Rates</strong><br/> County - 3.2273; Township - .62; PVSD (School + Library) = 23.8117
    </div>
    <div class="lower">
      <div class="pay">
        Pay Tax Bill Online<br/>
        <img alt="Pay Online" height="80" src="payonline.png" width="144">
      </div>

      <div class="pay">
        Understanding Your Tax Bill<br/>

        <button onclick="show('bills')" class="link"><img alt="Fall Bill" height="129" src="FallTaxBill-psd.png" width="300"></button>
      </div>
    </div>
  </div>
</div>

<div id="bills">
  <div class="mainContents">
    <div class="head">Understanding Your Tax Bill</div><br/>
    <div class="lower">
      <div class="pay">
        Spring Tax Bill<br/>
        <img class="img" src="http://onebigphoto.com/uploads/2012/10/midnight-sun-in-lofoten-norway.jpg" alt="Midnight sun in Lofoten, Norway" width="300" height="200">
      </div>
      <div class="pay">
        Fall Tax Bill<br/>
        <img class="img" src="http://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1490029386/fisherman-cabin-hamnoy-lofoten-islands-norway-NORWAY0320.jpg?itok=cpPuUjh1" alt="Fishermen's cabins in Lofoten, Norway" width="300" height="200">
      </div>
      <div class="pay">
        Fall Tax Bill Stubs<br/>
        <img class="img" src="http://fjordtours.blob.core.windows.net/fjordtours-umbraco/1199/gerirangerfjord-per-ottar-walderhaug-fjordnorway.jpg" alt="Gerirangerfjord, Norway" width="300" height="200">
      </div>
    </div>
  </div>
</div>

<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>

交换内容时移除监听器,改为在此处添加图像监听器:

var modal = document.getElementById('myModal');
var images = document.getElementsByClassName('img');
var modalImg = document.getElementById("img01");


var span = document.getElementsByClassName("close")[0];

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


function show(param_div_id) {
  document.getElementById('main').innerHTML = document.getElementById(param_div_id).innerHTML;

    for (var i = 0; i < images.length; i++) {
      var img = images[i];
      img.onclick = function(evt) {
        console.log(evt);
        modal.style.display = "block";
        modalImg.src = this.src;
      }
    }

}
.img {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

.img:hover {
  opacity: 0.8;
}

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 10;
  /* 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.9);
  /* Black w/ opacity */
}

.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

.modal-content,
#caption {
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

@media only screen and (max-width: 700px) {
  .modal-content {
    width: 100%;
  }
}

#main {
  background-image: url('bkgd.jpg');
  display: block;
  width: 690px;
  font-family: "Book Antiqua";
  color: #004B97;
  font-size: 16pt;
  padding: 2%;
  margin: 5px;
  border-radius: 15px;
  border: medium #9999FF solid;
}

.mainContents {
  display: flex;
  flex-wrap: wrap;
  margin: 0 auto;
  position: relative;
  width: 690px;
  flex-wrap: wrap;
  justify-content: center;
}

#home,
#bills,
#munTax,
#schoolTax,
#interimTax,
#taxCert,
#dupsReceipts,
#directions,
#FAQ {
  display: none;
  width: 300px;
  font-family: "Book Antiqua";
  color: #004B97;
}

.head {
  width: 100%;
  text-align: center;
  font-family: inherit;
  font-size: 16pt;
  color: #004B97;
  padding-bottom: 10px;
}

.head2 {
  position: relative;
  width: 100%;
  text-align: center;
  font-family: inherit;
  font-size: 14pt;
  color: #835BF9;
  padding-bottom: 15px;
}

.lower {
  width: 100%;
  display: flex;
  margin: 0 auto auto 10;
  flex-wrap: wrap;
  justify-content: space-around;
  padding: 10px;
  align-items: center;
}

.pay {
  text-align: center;
  background-image: url('background.jpg');
  color: #0D095E;
  font-size: 14pt;
  border: medium #9999FF solid;
  border-radius: 15px;
  padding: 5px;
  margin: 5px;
}
<div id="main">
  <div class="mainContents">
    <div class="head">Outside Receiving Hours:<br/> Please use secure drop box outside building
    </div>
    <div class="head2">
      <strong>2022 Millage Rates</strong><br/> County - 3.2273; Township - .62; PVSD (School + Library) = 23.8117
    </div>
    <div class="lower">
      <div class="pay">
        Pay Tax Bill Online<br/>
        <img alt="Pay Online" height="80" src="payonline.png" width="144">
      </div>

      <div class="pay">
        Understanding Your Tax Bill<br/>

        <button onclick="show('bills')" class="link"><img alt="Fall Bill" height="129" src="FallTaxBill-psd.png" width="300"></button>
      </div>
    </div>
  </div>
</div>

<div id="bills">
  <div class="mainContents">
    <div class="head">Understanding Your Tax Bill</div><br/>
    <div class="lower">
      <div class="pay">
        Spring Tax Bill<br/>
        <img class="img" src="http://onebigphoto.com/uploads/2012/10/midnight-sun-in-lofoten-norway.jpg" alt="Midnight sun in Lofoten, Norway" width="300" height="200">
      </div>
      <div class="pay">
        Fall Tax Bill<br/>
        <img class="img" src="http://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1490029386/fisherman-cabin-hamnoy-lofoten-islands-norway-NORWAY0320.jpg?itok=cpPuUjh1" alt="Fishermen's cabins in Lofoten, Norway" width="300" height="200">
      </div>
      <div class="pay">
        Fall Tax Bill Stubs<br/>
        <img class="img" src="http://fjordtours.blob.core.windows.net/fjordtours-umbraco/1199/gerirangerfjord-per-ottar-walderhaug-fjordnorway.jpg" alt="Gerirangerfjord, Norway" width="300" height="200">
      </div>
    </div>
  </div>
</div>

<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>