모달 팝업 상자를 만들려고하므로 아이콘을 클릭하면 다른 이미지가 나타납니다. 다른 이미지 대신 동일한 아이콘을 팝업으로 만 만들 수있었습니다. 저는 기본적으로 JS의 초보자이기 때문에 코드의 의미와 필요성에 대해서는 확신 할 수 없습니다. "cameraicon.svg"를 "canon.png"로 바꾸려고합니다.클릭시 모달 이미지 변경
이것은 내가 지금까지 무엇을 가지고 :
<img id="myImg" src="Images/cameraicon.svg" alt="This photo...." width="30" height="30">
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<img src="Images/canon.png" id="canon">
<div id="caption"></div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
function changeImage(img) {
document.getElementById("img").src = img.src.replace("myImg", "canon");
}
var img = document.getElementById('myImg');
var modalImg = document.getElementById("canon");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
여기에 JQuery가 없으며 모두 순수 JS입니다. JQuery를 포함 시켰을 때 - ** 그 생명의 은인 ** –
'document.getElementById ("img"). src' 나는 id가'img' 인 요소를 볼 수 없습니다. 이것은 작동하지 않습니다. 어떤 이미지를 타겟팅하려고합니까? –
죄송합니다. 나는 JS를 의미한다고 생각한다. "myImg"를 "canon"으로 대체하려고합니다. 코드를 이해할 수 없기 때문에 어디로 가고 싶은지 모르겠습니다. – webcat