如何在按钮单击时更改背景图像?我只设法改变了背景颜色。我是 js 和一般网络的新手。
var colorArray = ["#0DC2FF", "#007DE8", "#0E5DFF", "#0C28EB", "#1600FF"];
var i = 0;
function changeColor(){
document.body.style.background = colorArray[i];
i++;
if( i >= colorArray.length) {
i = 0;
}
}
.page {
width: 100%;
height: 700px;
display: flex;
align-items: center;
justify-content: center;
}
.button {
background: transparent;
border: 1px solid #000;
font-family: 'Montserrat', sans-serif;
color: #000;
font-weight: 900;
text-transform: uppercase;
padding: 1rem 3rem;
margin: 0 auto;
}
.button:hover {
background: #000;
color: #fff;
}
.button,
.button:active,
.button:focus {
outline: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<title>JavaScript</title>
</head>
<body>
<div class="page">
<button class="button" onclick="changeColor();">Click</button>
</div>
<script src="app.js"></script>
</body>
</html>
1 个回答