【发布时间】:2021-07-03 07:10:35
【问题描述】:
我编写了一个脚本,当您单击较小的图像时,它会在模态框中打开放大的图像,但由于某种原因,当我尝试单击小图像时,模态框没有打开。我的网站使用引导程序,但我已经编写了自己的 CSS 来创建模式,因为引导程序没有为我提供我想要的。
我在 Chrome 中打开了我的检查器,但我的 JavaScript 出错了
未捕获的类型错误:无法读取 null 的属性“addEventListener”
这是在 javascript 的第 1 行
HTML&PHP
<div class="bgmd">
<div class="ctmod">
<div class="xbutton">×</div>
<?php
echo "<img src='images/car22.jpg'/>";//Large Image
?>
</div>
</div>
<?php
echo "<a href='#' id='big'><img src='images/car".$id.".jpg' class='img-thumbnail center-block'/></a>";//Smaller Image
CSS
.bgmd{
width: 100% !important;
height: 100% !important;
background-color: rgba(0, 0, 0, 0.7) !important;
position: absolute !important;
top: 0 !important;
display: flex !important;
justify-content: center !important;
align-items: center !important;
display: none !important;
}
.ctmod{
width: 500px !important;
height: 300px !important;
background-color: white !important;
border-radius: 4px !important;
text-align: center !important;
position: relative !important;
}
.xbutton{
position: absolute !important;
top: 0 !important;
right: 14px !important;
font-size: 42px !important;
transform: rotate(45deg) !important;
cursor: pointer !important;
}
JAVASCRIPT
document.getElementById('big').addEventListener('click', function() {
document.querySelector('.bgmd').style.display = 'flex';
});
document.querySelector('.xbutton').addEventListener('click', function() {
document.querySelector('.bgmd').style.display = 'none';
});
【问题讨论】:
-
似乎
document.getElementById('big')或document.querySelector('.xbutton')是null。可以贴出生成的 HTML 代码吗? -
因为错误发生在第 1 行,所以应该是
document.getElementById('big')为空...
标签: javascript html css