
document.title = "Hello, world!";
Uncaught TypeError : Cannot set property 'innerText' of nullconst title = document.getElementById("title");
title.innerText = "Got You!";
const hellos = document.getElementsByClassName("hello");
console.log(hellos);
element를 CSS 방식으로 검색
(.클래스 원하는요소) : hello 란 class 내부에 있는 h1을 가지고 올 수 있음 ( 못 찾을 경우 null 반환 )querySelectorAllconst title = document.querySelector(".hello h1");
title.innerText = 'Click ME!!!';
querySelector('')
getElementById('')
-> 위 두개 모두 하는 일은 같음
-> BUT, querySelector('') 는 h1을 가져오거나 하위의 form을 가져 올 수 있음. getElementById('')는 불가함
이벤트 발생
title.style.color = 'tomato';
function handleTitleClick() {
console.log("title was clicked!");
title.style.color = 'red';
};
title.addEventListener('click', handleTitleClick);
// 같은 표현
title.addEventListener('click', handleTitleClick);
title.onClick = handleTitleClick;
: 마우스 호버시 이벤트
function handleMouseEnter() {
console.log("Mouse is here");
title.innerText = "Mouse is here!";
title.style.color = "green";
}
title.addEventListener('mouseenter', handleMouseEnter);
// title.onmouseenter = handleMouseEnter;
: 마우스 호버 아웃시 이벤트
function handleMouseLeave() {
console.log("Mouse is leaved");
title.innerText = "Mouse is gone!";
title.style.color = "blue";
}
title.addEventListener('mouseleave', handleMouseLeave);
: 창 크기 조절시 이벤트 발생
function handleWindowResize() {
document.body.style.backgroundColor = "lightgray";
};
window.addEventListener("resize", handleWindowResize);
: 복사-붙여넣기 시 이벤트 발생
function handleWindowCopy() {
alert("copier");
};
window.addEventListener("copy", handleWindowCopy);
: 인터넷 연결 안될 경우 이벤트 발생
function handleWindowOffline() {
alert("NO WIFI")
};
window.addEventListener("offline", handleWindowOffline);
: 인터넷 연결시 이벤트 발생
function handleWindowONline() {
alert("All GOOD")
};
window.addEventListener("online", handleWindowONline);
https://codesandbox.io/s/day-three-blueprint-forked-ql6qzz
