弹出模型框中的未定义图像src

时间:2018-11-14 05:35:14

标签: javascript

我正在制作带有模型弹出框的图像库,JavaScript代码正在运行,但是库弹出模态框的未定义图像src和内容框中未显示弹出图像。我犯了什么错误?非常感谢您的帮助。

<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" onclick="imgPopup();" alt="Snow" style="width:100%;max-width:300px">'; ?>


function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
modal.style.display = "block";
modalImg.src = this.src;

var span = document.getElementsByClassName("close")[0];
span.onclick = function() { 
    modal.style.display = "none";
} }

1 个答案:

答案 0 :(得分:0)

请尝试

<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" onclick="imgPopup(this);" alt="Snow" style="width:100%;max-width:300px">'; ?>

function imgPopup(elem){
    var modal = document.getElementById('myModal');
    var modalImg = document.getElementById("myImg1");

    modal.style.display = "block";
    modalImg.src = elem.src;

    var span = document.getElementsByClassName("close")[0];
    span.onclick = function() { 
        modal.style.display = "none";
    } 
}