您好,我想创建一个随机图像生成器网站,最近在网上找到了适合我意图的JSfiddle文件。我想使用CSS和JS将该JSfiddle文件传输到HTML。当我在JSfiddle中运行它时,它可以正常工作,但是当我将其应用于实际网站时,它只能识别HTML和CSS,而不能识别JS,这意味着它看起来不错,但不会生成随机图像。我对编码有点陌生,不知道出了什么问题,请在下面查看我的编码。非常感谢。
HTML
https://jsfiddle.net/alicevo12/4xs7ebao/show/
<!DOCTYPE html>
<html>
<head>
<title>Random</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="style.js"></script>
</head>
<body>
<div id="banner-message">
<p>Hello World</p>
<button id="button">Change image</button>
</div>
<div id="img"></div>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
}
header{
background-image:linear-gradient(rgba(0,0,0,0.8),rgba(0,0,0,0.8)), url(../Arcane/Images/thefoollabel.png);
height: 100vh;
background-size: cover;
background-position: center;
}
ul{
float: right;
list-style-type: none;
margin-top: 25px;
}
ul li{
display: inline-block;
}
ul li a{
text-decoration: none;
color: #fff;
padding: 5px 20px;
border: 1px solid transparent;
transition: 0.4s ease;
}
ul li a:hover{
background-color: #fff;
color: black;
}
ul li.active a{
background-color: #fff;
color: black;
}
.logo img{
float: left;
width: 150px;
height: auto;
margin-top: 20px;
}
.main{
max-width: 1200px;
margin: auto;
}
.title{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.title h1{
font-size: 70px;
color: #fff;
}
.subtitle{
position: absolute;
text-align: center;
top: 60%;
left: 50%;
transform: translate(-50%,-50%);
}
.subtitle h1{
font-family: Helvetica;
font-weight: 500;
font-size: 20px;
color: #bebc53;
}
.subsubtitle{
position: absolute;
text-align: center;
top: 66%;
left: 50%;
transform: translate(-50%,-50%);
}
.subsubtitle h1{
font-family: Helvetica;
font-style: italic;
font-weight: 500;
font-size: 20px;
color: #bebc53;
}
.button{
position: absolute;
top: 75%;
left: 50%;
transform: translate(-50%,-50%);
}
.btn{
border: 1px solid #fff;
padding: 10px 30px;
color: #fff;
font-size: 20px;
text-decoration: none;
transition: 0.4s ease;
}
.btn:hover{
background-color: #fff;
color: black;
}
JS
var images =[
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
// Set default image for load
document.getElementById('img').style.backgroundImage = "url('" + images[0] + "')";
var button = document.getElementById("button").addEventListener("click", function(){
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('img').style.backgroundImage = "url('" + images[r] + "')";
});
JSFiddle默认执行onload。您需要将JS包装在加载处理程序中
const images = [
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
window.addEventListener("load",function() {
document.getElementById('img').style.backgroundImage = "url('" + images[0] + "')";
document.getElementById("button").addEventListener("click", function(){
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('img').style.backgroundImage = "url('" + images[r] + "')";
});
})
此代码可以放在头中,也可以在外部JS文件和main.css中的CSS中
const images = [
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
window.addEventListener("load", function() {
document.getElementById('imgDiv').style.backgroundImage = "url('" + images[0] + "')";
document.getElementById("button").addEventListener("click", function() {
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('imgDiv').style.backgroundImage = "url('" + images[r] + "')";
});
})
* {
margin: 0;
padding: 0;
}
header{
background-image:linear-gradient(rgba(0,0,0,0.8),rgba(0,0,0,0.8)), url(../Arcane/Images/thefoollabel.png);
height: 100vh;
background-size: cover;
background-position: center;
}
ul {
float: right;
list-style-type: none;
margin-top: 25px;
}
ul li {
display: inline-block;
}
ul li a {
text-decoration: none;
color: #fff;
padding: 5px 20px;
border: 1px solid transparent;
transition: 0.4s ease;
}
ul li a:hover {
background-color: #fff;
color: black;
}
#imgDiv { min-height: 1000px; }
<div id="banner-message">
<p>Hello World</p>
<button id="button">Change image</button>
</div>
<div id="imgDiv">Test
</div>
欢迎使用Stackoverflow,>>
代码是正确的,但是您要做的就是在正文的末尾添加<script>
标记以加载script
文件,javascript无法获取img
div,因为dom元素尚未加载。
<script src="style.js"></script>
</body>