gpt4 book ai didi

javascript - 控制台日志返回null

转载 作者:行者123 更新时间:2023-12-03 00:01:55 26 4
gpt4 key购买 nike

我尝试做一个弹出窗口,当我在 html 中执行 js 时,它工作得很好,但是当我尝试将 js 与 html 分开时,我的 js 文件不起作用,我不知道为什么。 。我不知道问题是我如何调整我的js还是我的js如何链接如果有人可以提供帮助,那就太好了!

这是我的代码,如果还有什么需要问我! :

    
var modal = document.getElementById('popup2');

var essai = document.getElementById('id02');
console.log(modal)

essai.addEventListener("click", function () {

modal.style.display = "block";

})

var span2 = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span2.onclick = function () {
essai.style.display = "none";
}
.popup2{z-index: 3;
display: none;
padding-top: 100px;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.82);
}
<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">

<title>Nejma Hamadi</title>

<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="vendor/bootstrap/css/styles.css" rel="stylesheet">
<link href="vendor/bootstrap/css/stylesGallery.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="css/scrolling-nav.css" rel="stylesheet">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

</head>

<body id="page-top">


<section id="portfolio2">


<img src="img/img2.jpg" id="img1">

<div id="id02" class="popup2">
<div>
<span class="close">&times;</span>
<img src="img/img3.jpg" alt="Avatar" style="width:30%" id="img001">
<img src="img/img3.jpg" alt="Avatar" style="width:30%">
<img src="img/img3.jpg" alt="Avatar" style="width:30%">
</div>
</div>


</section>












<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- Plugin JavaScript -->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/magnific-popup.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/jquery.magnific-popup.min.js"></script>

<!-- Custom JavaScript for this theme -->
<script src="js/scrolling-nav.js"></script>
<!--<script src="js/popup.js"></script>-->
<script src="js/popup2.js"></script>
<script src="js/popupVideo.js"></script>


</body>

</html>

最佳答案

在 HTML 中,您使用 popup2 作为类名,而不是 ID!因此,您应该将 var modal = document.getElementById('popup2'); 替换为

var modal = document.getElementsByClassName('popup2')[0];

另一件事:确保此代码在 HTML 加载后运行,否则它将返回 undefined。为此,请将代码包装在里面:

document.onload = function() {
var modal = document.getElementsByClassName('popup2')[0];
//your code here
}

关于javascript - 控制台日志返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55131086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com