问题描述
让我有点疯狂,应该很容易为所有兽医弄清楚。
简而言之,如果我只是删除然后粘贴回Javascript文件位置行<script src="/JS/scripts.js"></script>
(有时必须执行几次),则该代码可在浏览器预览中工作(有时必须执行几次),然后突然在浏览器中工作预览,但从未在Dreamweaver中实时显示。如果我保存所有文件并重新加载,代码将再次停止工作。
如果代码全部包含在HTML文件中,则代码似乎可以正常工作,但是一旦我将脚本分离到外部文件中,所有的内容就会崩溃。任何见解/帮助将不胜感激。提前致谢。这是代码:
HTML:
<!DOCTYPE html>
<html>
<head>
<Meta name="viewport" content="width=device-width,initial-scale=1">
<link href="CSS/styles.css" rel="stylesheet" type="text/css">
<script src="JS/scripts.js"></script>
</head>
<body>
<h2>Image Modal</h2>
<p>In this example,we use CSS to create a modal (dialog Box) that is hidden by default.</p>
<p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p>
<img src="images/thumbnail4.jpg" id="myImg" alt="Testes123" style="width:100%;max-width:300px">
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
</body>
</html>
JAVASCRIPT:
// JavaScript Document
// 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";
}
CSS:
body {font-family: Arial,Helvetica,sans-serif;}
#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); /* Fallback color */
background-color: rgba(0,0.9); /* Black w/ opacity */
}
/* Modal Content (image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* Caption of Modal Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.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%;
}
}
解决方法
好吧,我想我自己解决了这个问题-如果脚本位置行位于文档的开头,它将失败,因此脚本位置行必须位于模式代码之后的正文末尾才能正常运行。