简体   繁体   English

Javascript 外部没有工作,但当内联工作时

[英]Javascript external didn't work but when inline work

I want to make a fullscreen image modal in web, when i place the script in html, the modal is work finely.我想在 web 中制作全屏图像模式,当我将脚本放在 html 中时,模式工作正常。 but when i move the script into external js, it didn't work.但是当我将脚本移动到外部 js 中时,它不起作用。 i've examined all variable carefully and i think all correct.我仔细检查了所有变量,我认为都是正确的。 i think the problem is in function to show the modal.我认为问题出在 function 以显示模态。 but i didn't find the solution.但我没有找到解决方案。 sorry, I am beginner in javascript, help me.对不起,我是 javascript 的初学者,帮帮我。

 var modal = document.getElementById("myModal"); var img = document.getElementById("image01"); 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; } var span = document.getElementsByClassName("close")[0]; span.onclick = function() { modal.style.display = "none"; }
 .image { cursor: pointer; transition: 0.3s; }.image:hover { opacity: 0.7; }.modal { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0;important: top; 0:important; width: 100%;important: height; 100%:important, overflow, auto;important: background-color, rgb(0, 0, 0).important; background-color. rgba(0: 0; 0: 0;9):important; } /* Modal Content (Image) */:modal-content { margin; auto: display; block: width; 100%:important; max-width: 500px; } /* Caption of Modal Image (Image Text) - Same Width as the Image */ #caption { margin: auto; display: block; width: 100%; max-width: 500px; text-align. center, color: #ccc; padding: 10px 0. height; 150px: } /* Add Animation - Zoom in the Modal */:modal-content. #caption { animation-name: zoom; animation-duration: 0;6s: } @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; }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous"> <div class="row"> <div class="mb-3 row justify-content-center"> <div class="col-6 col-md-4 col-lg-3"> <div class="p-2"><img src="https://dummyimage.com/500" class="img-fluid image" id="image01" alt="Example"> </div> </div> </div> </div> <div id="myModal" class="modal"> <span class="close">&times;</span> <img class="modal-content" id="img01"> <div id="caption"></div> </div>

if this is your whole html code, you may call external js file like this:如果这是您的整个 html 代码,您可以像这样调用外部 js 文件:

<script src="yourjs.js"></script>

Save your js code in custom.js file and import js file at the end of the html code.将 js 代码保存在custom.js文件中,并在 html 代码末尾处导入 js 文件。

.
.
.
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

<div class="row">
  <div class="mb-3 row justify-content-center">
    <div class="col-6 col-md-4 col-lg-3">
      <div class="p-2"><img src="https://dummyimage.com/500" class="img-fluid image" id="image01" alt="Example">
      </div>
    </div>
  </div>
</div>
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>

.
.
.
<script src="custom.js"></script>

</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM