for(){
div.onclick=function(){
if(){
怎样点击变色 取消变色
}
}
}
改变背景颜色用
box.style.backgroundColor
改变字体颜色
box.style.color
// div-test为元素id
document.getElementById('div-test').style.color = "red"
document.getElementById('div-test').style.backgroundColor = "red"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#box {
width: 200px;
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div id="box" onclick="boxClick()"></div>
<script>
let box = document.getElementById("box");
function boxClick() {
if (box.style.backgroundColor === "red") {
box.style.backgroundColor = "black";
} else {
box.style.backgroundColor = "red";
}
}
</script>
</body>
</html>