简体   繁体   中英

how to make onclick function work not only once but each time i click it in pure vanila javascript

How to make onclick function work not only once but each time i click it in pure vanila javascript? When I click once the color of body changes, but when i click once more nothing happens...Also im unsure how to set this "change color" in the center. Tried everything just cant make it look right. My css is really bad here but im practicing js. Sorry for being a pure noob...

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Your variable randomNumber is only set once. You need set a new random number each time the function is run.

Also, I recommend using function rather than const for functions and methods.

Your not recalculating a random number every time you click, it's changing the color but back to the same everytime

so move random_number = Math.floor(Math.random()*16777215).toString(16); into your setBackground function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM