javascript - 为不同的模态获取相同的图像
问题描述
即使我有不同的类,我也无法在我的模态中获取相同的图像......我确定这是我的 JS 代码,只是无法弄清楚......非常感谢任何帮助。
我还在学习 JS,但知道的就够危险了!我确信这不是最好的编码方式,但我从 W3schools 获取了基础知识并根据我的需要进行了调整。
起初只有一个模式可以工作,但我想出了如何使代码更详细,现在我得到了相同的图片。
我如何确保它选择了正确的?
这是我的代码:
// 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 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";
}
}
// Get the modal
var modal2 = document.getElementById("myModal2");
// Get the button that opens the modal
var btn = document.getElementById("myBtn2");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close2")[0];
// When the user clicks 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>
<button id="myBtn">1</button> <!-- The Modal -->
<div id="myModal" class="modal"><!-- Modal content -->
<div class="modal-content">
<img class="img" src="https://bbk12e1-cdn.myschoolcdn.com/ftpimages/772/misc/misc_190683.123dx" />
<span class="close">×</span>
<div>This is the Galbraith Building</div>
</div>
</div>
<button id="myBtn2">2</button> <!-- The Modal -->
<div id="myModal2" class="modal2"><!-- Modal content -->
<div class="modal-content2">
<img class="img2" src="https://bbk12e1-cdn.myschoolcdn.com/772/photo/2015/06/orig_photo275070_2980857.jpg" />
<span class="close2">×</span>
<div>This is the Galbraith Building</div>
</div>
解决方案
您正在为这两个功能调用第一个模式。将第二组更改为 modal2.style.display ,它应该可以解决您的问题
// 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 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";
}
}
// Get the modal
var modal2 = document.getElementById("myModal2");
// Get the button that opens the modal
var btn = document.getElementById("myBtn2");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close2")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal2.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal2.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal2.style.display = "none";
}
}
推荐阅读
- swift - 使用 Accelerate 框架执行规范化
- python - 用python创建一对用户ID
- python - TypeError:float() 参数必须是字符串或数字,而不是 'ImmutableMultiDict'
- javascript - JS 客户端 Cookie 在移动浏览器上过早过期
- php - PHP - 二维数组 json_encode 无法正常工作?
- azure - 如何将音频文件上传到 Azure 并将它们转换为文字?
- sql - 获取每个 firstName-lastName 组的最新条目,最长年份
- java - 在 Intellij 中执行 bash 命令不起作用
- javascript - 我的 Javascript 代码中没有显示输出
- python - 静态构建python以嵌入C++