javascript - 错误 : Uncaught TypeError: Cannot set property 'onclick' of null

标签 javascript php html css

请帮忙解决问题: 我想在点击时通过弹出窗口修改图片的输出。 有代码(原创):

<div class="spproperty-floor-img">
<img alt="" src="<?php echo JUri::root() . $floor_plan['img']; ?>">
</div>

使用javascript、css和html+一些php修改代码:

// 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("img01");
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";
}
/* Style the Image Used to Trigger the Modal */
#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (Image) */
.modal-content {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
}

/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 150px;
}

/* Add Animation - Zoom in the Modal */
.modal-content, #caption { 
    -webkit-animation-name: zoom;
    -webkit-animation-duration: 0.6s;
    animation-name: zoom;
    animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
    from {-webkit-transform:scale(0)} 
    to {-webkit-transform:scale(1)}
}

@keyframes zoom {
    from {transform:scale(0)} 
    to {transform:scale(1)}
}

/* The Close Button */
.close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: #bbb;
    text-decoration: none;
    cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
    .modal-content {
        width: 100%;
    }
}
<div class="property-floor-img">
<img id="myImg" src="<?php echo JUri::root() . $floor_plan['img']; ?>" alt="">
<div id="myModal" class="modal">
<span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
<div id="caption"></div>
</div>
</div>     

Javascript 返回错误: “未捕获的类型错误:无法设置 null 的属性‘onclick’

这是怎么回事? 预先感谢您的帮助!

最佳答案

您没有 id 为 img01 的元素。

var img = document.getElementById('myImg');

是的,我没有添加它。 现在的代码是:

<div class="spproperty-floor-img"> 
  <img id="myImg" src="<?php echo JUri::root() . $floor_plan['img']; ?>" alt="">                                           
  <div id="myModal" class="modal">
  <img class="modal-content" id="img01">
  <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
  <div id="caption"></div>
  </div>
  </div>

但是还是在控制台报错: 未捕获的类型错误:无法设置 null 的属性“onclick” 在 javascript 的这一行:

img.onclick = function(){

关于javascript - 错误 : Uncaught TypeError: Cannot set property 'onclick' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346694/

相关文章:

php - 查找特定目录名称的有效方法?

javascript - 如何使用 Snap SVG 在路径上制作动画?

javascript - 小谜语: Why this isn't working on Chrome?

javascript - listRef.listAll() 函数未定义(react-native-firebase)

javascript - 使用 div 的两种方式数据绑定(bind)

php - Android:无法从真实网站下载大尺寸数据库到 SQLite 数据库

php - 使用 PHP 框架会影响性能吗?

html - 如何在CSS中将水平列表对齐到中心

jquery - 类型错误 : variable is undefined; i get this error in specific pages

java - 使用 Java 填充 HTML 文件中的占位符