<!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>换背景</title>
</head>
<body>
<!-- 点击就把a取反的结果传给函数,函数接受判断 -->
<button onclick="dian(a = !a)">切换</button>
<script>
const body = document.querySelector('body')
//设置a初始值
let a = false
function dian(a){
if(a){
body.style. backgroundColor= 'black';
}else{
body.style. backgroundColor= 'white';
}
}
</script>
</body>
</html>