Created
May 29, 2018 02:53
-
-
Save yukeehan/ecf3041bac19703c4f71b602e41a2826 to your computer and use it in GitHub Desktop.
use addEventListener to listen the click and change the background color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>color changer</title> | |
<style type="text/css"> | |
button{ | |
font-size: 20px; | |
margin: 50px; | |
} | |
.ispink{ | |
background: pink; | |
} | |
</style> | |
</head> | |
<body> | |
<button>click me!</button> | |
<script type="text/javascript" src="colorchanger.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var button = document.getElementsByTagName("button")[0]; | |
// var ispink = false; | |
// button.addEventListener("click", function(){ | |
// if(!ispink){ | |
// document.body.style.background = "pink"; | |
// } | |
// else{ | |
// document.body.style.background = "white"; | |
// } | |
// ispink = !ispink; | |
// }); | |
button.addEventListener("click",function(){ | |
document.body.classList.toggle("ispink"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment