使用图像和标题使JavaScript onclick功能打开模态

时间:2017-06-11 18:59:53

标签: javascript jquery html

我目前拥有它以便onClick模式打开,但它不显示图像.. 这是代码
HTML

<div class="item">
<img src="Imgs/10.jpg" alt="Hello welcome to the picture thingy" onclick="showImage('Imgs/10.jpg');" style="width:100%;" id="myImg" />
<p class="xtratekst">
othertext
</p></div><div id="myModal" class="modal"> 
<!-- The Close Button -->
<span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
!-- Modal Content (The Image) -->
<img class="modal-content" id="image">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>

的JavaScript

// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("image");
var captionText = document.getElementById("caption");
function showImage(){
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";
}

1 个答案:

答案 0 :(得分:1)

showImage函数应该带一个参数:

function showImage(image){

然后将模态图像src设置为该参数:

modalImg.src = image