<input type="button" value="关灯"></input>
<script>
var getbody = document.body; //获取body元素
var getbutton = document.querySelector('input'); //获取按钮元素
var flag = true; //设置一个变量
getbutton.onclick = function() {
if (flag == true) {
getbody.style.backgroundColor = 'black';
flag = false;
getbutton.value = '开灯';
} else {
getbody.style.backgroundColor = '#fff';
flag = true;
getbutton.value = '关灯';
}
}
</script>