如何使用 addEventListener 'click' 的按钮运行 2 个不同的代码,以在 Javascript 中用作开/关功能?



我有一个将页面背景变黑的"点击"事件,我需要它在再次单击相同的按钮后变白......

const lightButton = document.getElementById('sleep');
lightButton.addEventListener('click', () => {
Screen.style.background = 'black';

如何添加此代码,以便在再次单击按钮后屏幕变为白色?

Screen.style.background = 'white';
LightButton.addEventListener('click', () => {    
if (Screen.style.background = 'black') {
Screen.style.background = 'white';
} else {
Screen.style.background = 'black';
}
}

const screen = document.getElementById('screen'(; const lightButton = document.getElementById('sleep'(;

lightButton.addEventListener('click', function() {
if (screen.style.backgroundColor == 'black') {
screen.style.backgroundColor = null;
} else {
screen.style.backgroundColor = 'black';
}
});

最新更新