파게로그

event, event handlers (2) 본문

콤퓨타 왕기초/JS

event, event handlers (2)

파게 2020. 10. 12. 10:43
const hi1 = document.querySelector("#hi1"); // const hi1 = document.getElementById("hi1"); const BASE_COLOR = "rgb(52, 73, 94)"; const OTHER_COLOR = "#7f8c8d"; var cnt = 0; function handleClick() { ​​​​const currentColor = hi1.style.color; ​​​​if (currentColor == BASE_COLOR) { ​​​​​​​​hi1.style.color = OTHER_COLOR; ​​​​} else { ​​​​​​​​hi1.style.color = BASE_COLOR; ​​​​} } function handleMouseenter() { ​​​​const currentText = hi1.innerHTML; ​​​​if (currentText == "Hi, I'm from JS.") { ​​​​​​​​hi1.innerHTML = cnt+"times"; ​​​​​​​​cnt++; ​​​​} else { ​​​​​​​​hi1.innerHTML = cnt+"times"; ​​​​​​​​cnt++; ​​​​} } function init() { ​​​​hi1.innerHTML = "Hi, I'm from JS."; ​​​​hi1.style.color = "red"; ​​​​hi1.addEventListener("click", handleClick); ​​​​hi1.addEventListener("mouseenter", handleMouseenter); } init();

'콤퓨타 왕기초 > JS' 카테고리의 다른 글

DOM에서 element 생성 또는 제거  (0) 2020.10.15
array  (0) 2020.10.15
event, even handlers (1)  (0) 2020.10.12
DOM 기초  (0) 2020.10.12
자바스크립트 기초  (0) 2020.10.12
Comments