每个 <a> 属性的模态屏幕

Modal screen for each <a> attribute

我在学习使用的过程中遇到了自己的不足JavaScript。我花了几个小时寻找答案。

我正在尝试为每个城市创建一个弹出模式屏幕。但是,我的教程实施 (1) 不会为每个城市“弹出”模式 (2) 模式仅适用于第一个 <a id=myBtn ...> (3) 当我单击“伦敦”时,工作模式显示所有三个城市的描述" 第一次 (4) 并且只有在单击其他城市,然后单击“伦敦”后才会显示正确的城市描述。

我希望当我点击相应的城市名称时,城市描述出现在模态屏幕中。

如果你运行下面的代码片段,你会看到我的缺点。

var modal = document.getElementById("myModal"); // Get the modal
var btn = document.getElementById("myBtn"); // Get the button that opens the modal
var span = document.getElementsByClassName("close")[0]; // Get the <span> element that closes the modal

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}


function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";



}
body {font-family: Arial, Helvetica, sans-serif;}

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

/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

/* The Close Button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<!-- Clickable words -->
<a id="myBtn" class="tablinks" onclick="openCity(event, 'London')">London</a>
<br>
<a id="myBtn" class="tablinks" onclick="openCity(event, 'Paris')">Paris</a>
<br>
<a id="myBtn" class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</a>


<!-- Modal Contents -->
<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    
      <div id="London" class="tabcontent">
        <h3>London</h3>
        <p>London is the capital city of England.</p>
      </div>

      <div id="Paris" class="tabcontent">
        <h3>Paris</h3>
        <p>Paris is the capital of France.</p> 
      </div>

      <div id="Tokyo" class="tabcontent">
        <h3>Tokyo</h3>
        <p>Tokyo is the capital of Japan.</p>
      </div>
  </div>

</div>

Due credit: I have integrated two examples made by w3schools.

问题是您不能多次使用 ID,它不会起作用。为了让它工作,我会像这样在 openCity() 中放置打开模式的函数:

function openCity(evt, cityName) {
  modal.style.display = "block"; //Opening modal after click

  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
  }

然后每次点击任何link都会打开模式。