[英]background-color changing(onclick) via javascript not working
我试图在 javascript 中编写背景更改效果,单击按钮将更改背景颜色。问题是我应该使用 colors 数组,这将是背景颜色。 在每次单击按钮时,应将 colors 循环通过阵列。 这是下面的代码: -
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>background-changer</title>
</head>
<body>
<style>
body{
display: flex;
justify-content: center;
align-items: center;
}
button{
color: white;
background-color: black;
font-weight: 400;
font-family: sans-serif;
font-size: 28px;
border: 0;
border-radius: 5px;
padding: 1rem 2rem;
}
button:hover{
background-color: rgba(0, 0, 0, 0.8);
}
</style>
<button onclick="ChangeBackground()">CHANGE COLOR</button>
<script>
const btn = document.querySelector('button');
var backgrounds = ['red', 'royalblue', 'green', 'purple', 'yellow'];
function ChangeBackground(){
backgrounds.forEach(strtChng);
}
function strtChng(){
let i=0;
if(i<=backgrounds.length){
i++;
document.body.style.backgroundColor = backgrounds[i];
console.log(backgrounds[i]);
}
else{
i=0;
}
}
</script>
</body>
</html>
您循环遍历元素两次,而没有必要
const btn = document.querySelector('button'); var backgrounds = ['red', 'royalblue', 'green', 'purple', 'yellow']; let i = 0; function ChangeBackground(){ document.body.style.backgroundColor = backgrounds[i]; (i > backgrounds.length-2)? i = 0: i++ }
body{ display: flex; justify-content: center; align-items: center; } button{ color: white; background-color: black; font-weight: 400; font-family: sans-serif; font-size: 28px; border: 0; border-radius: 5px; padding: 1rem 2rem; } button:hover{ background-color: rgba(0, 0, 0, 0.8); }
<html lang="en"> <body> <button onclick="ChangeBackground()">CHANGE COLOR</button> </body>
我对您的代码进行了一些更改。
看看片段。
const btn = document.querySelector('button'); var backgrounds = ['red', 'royalblue', 'green', 'purple', 'yellow']; var count = 0; function ChangeBackground(){ if(count<=backgrounds.length){ count++; }else { count=0; } strtChng(count); } function strtChng(i){ document.body.style.backgroundColor = backgrounds[i]; console.log(backgrounds[i]); }
body{ display: flex; justify-content: center; align-items: center; } button{ color: white; background-color: black; font-weight: 400; font-family: sans-serif; font-size: 28px; border: 0; border-radius: 5px; padding: 1rem 2rem; } button:hover{ background-color: rgba(0, 0, 0, 0.8); }
<button onclick="ChangeBackground()">CHANGE COLOR</button>
使用addEventListener
和modulo (%)
的较短版本
let backgrounds = ['red', 'royalblue', 'green', 'purple', 'yellow'], i = 0 document.querySelector('button').addEventListener('click', () => { document.body.style.backgroundColor = backgrounds[i%backgrounds.length] i++ })
body { display: flex; justify-content: center; align-items: center; } button { color: white; background-color: black; font-weight: 400; font-family: sans-serif; font-size: 28px; border: 0; border-radius: 5px; padding: 1rem 2rem; } button:hover { background-color: rgba(0, 0, 0, 0.8); }
<button>CHANGE COLOR</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.