在学习使用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">×</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>
应有的荣誉:我整合了w3schools的两个示例。
答案 0 :(得分:1)
问题是您不能多次使用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";
}
然后,每次您单击任何链接时都会打开模态。