<!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>
div {
width: 80px;
height: 40px;
line-height: 40px;
text-align: center;
border: 1px solid red;
background-color: red;
}
</style>
</head>
<body>
<div>no</div>
<script>
var c = document.querySelector('div')
var d = 1;
c.onclick = function () {
d++;//每次让d加1
if (d % 2 == 0) {//初始值为红,当d除以2等于零时点击变黑,否则变红
c.style.backgroundColor = 'black'
c.style.color = 'red'
} else {
c.style.backgroundColor = 'red'
c.style.color = 'black'
}
}
</script>
</body>
</html>
js简易开关
最新推荐文章于 2024-05-16 10:03:01 发布