简体   繁体   English

为不同的模态获取相同的图像

[英]Getting same image for different modals

I'm having trouble getting the same image in my modals even though I have different classes on them...I'm sure it's my JS code, just can't figure it out....any help is much appreciated.即使我有不同的类,我也无法在我的模态中获得相同的图像......我确定这是我的 JS 代码,只是无法弄清楚......任何帮助都非常感谢。

I'm still learning JS, but know just enough to be dangerous!我仍在学习 JS,但知道足够危险! I'm sure this is not the best way to code, but I took the basics from W3schools and adjusted it to my needs.我确信这不是最好的编码方式,但我从 W3schools 获取了基础知识并根据我的需要进行了调整。

At first only one modal would work, but I figured out how to make the code more detailed, now I just get the same pic.起初只有一个模态可以工作,但我想出了如何使代码更详细,现在我得到了相同的图片。

How do I make sure it picks the right one?我如何确保它选择正确的?

Here is my code:这是我的代码:

 // 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">&#215;</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">&#215;</span> <div>This is the Galbraith Building</div> </div>

You're calling the first modal for both functions.您正在为这两个函数调用第一个模态。 Change the 2nd set to modal2.style.display and it should fix your issue将 2nd set 更改为 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";
  }
}

Although @Halmon fixed you specific issue, i think you should not repeat complete lines of code to do the same thing.尽管@Halmon 解决了您的特定问题,但我认为您不应该重复完整的代码行来做同样的事情。

Generic demo can be something like that (i put notes inside):通用演示可以是这样的(我把笔记放在里面):

 // this is your modal let modalCon = document.querySelector('#myModal'); // this is an array of images src. it may be helpful if you have more then 2 images. let chosen = [ 'https://bbk12e1-cdn.myschoolcdn.com/772/photo/2015/06/orig_photo275070_2980857.jpg', 'https://bbk12e1-cdn.myschoolcdn.com/ftpimages/772/misc/misc_190683.123dx' ]; // since the number of buttons that will trigger the modal // is the same as the array indexes you can simply: // a. add event listener when a button clicked document.querySelectorAll('#modalTrig button').forEach((btn,i) => btn.addEventListener('click',()=> { // b. set the src of modal's image document.querySelector('#myModal img').setAttribute('src', chosen[i]); // c. show the modal modalCon.classList.add('show'); })); // this will close the modal document.querySelector('#close').addEventListener('click', () => { modalCon.classList.remove('show'); });
 /* just for demo */ #myModal { width: 85%; max-width: 600px; margin: auto; opacity: 0; transition: 1s ease-in-out; } #myModal img {max-width: 100%; height: auto;} #myModal.show { opacity: 1;}
 <nav id="modalTrig"> <button>1</button> <button>2</button> </nav> <aside id="myModal" class="modal"><!-- Modal content --> <button id="close">&#215;</button> <figure class="modal-content"> <img id="img" /> <figcaption>This is the Galbraith Building</figcaption> </figure> </aside>

PS - although learning from examples can be quicker you should learn the basics before. PS - 虽然从示例中学习可以更快,但您应该先学习基础知识。 Start here .这里开始。

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

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