+ 3
More than one event
I want to add more than one css background to js script (document.getElementById('back'). style.backgroundColor='black'") But now I want to change it back to white or none
4 ответов
+ 7
You can use flag or classList API toggle method
https://www.sololearn.com/post/276988/?ref=app
https://www.sololearn.com/post/276990/?ref=app
+ 2
I'm slightly unclear on exactly how you want this to perform.
I'm answering under the following assumption: You've setup an event listener - the initial action when the event occurs is changing the background color to black - now you'd like the NEXT time the event occurs to change to white or none.
The simplest way I could think of is to edit your event listener function with a simple boolean value that you change each time the function is called. Then place your code to change the background color inside an if/else statement to change depending on the value of that boolean variable.
For example a variable called "isBlackBg" that is false to start.
if(isBlackBg) {
// change background color to white/other
isBlackBg = false;
} else {
// change background color to black
isBlackBg = true;
}
1st Event) -- Change background color to black --set isBlackBg to true
2nd Event) -- Change background color to white(or other) -- set isBlackBg to false
3rd Event) -- Change background color to black --set isBlackBg to true
As you can see it will continuously switch back and forth between false and true switching the color based on that value.
- 1
Hello