브라우저에서 그냥 사용할 수 있는 'document'라는 object를 배웠다.
document의 함수 중에는 getElementById라는 함수가 있는데,
이 함수가 HTML에서 id를 통해 element를 찾아준다.
element를 찾고 나면, JS로 해당 HTML의 무엇이든 바꿀 수 있다.
ex. element의 innerText를 바꾼다던가 (title.innerText = "Got you!";)
id, className 등을 가져올 수 있음. (cosole.log(title.id);)
getElementsByClassName() : 많은 element를 가져올때 씀(array를 반환)
getElementsByTagName() : name을 할당할 수 있음(array를 반환)
querySelector : element를 CSS selector방식으로 검색할 수 있음 (ex. h1:first-child)
단 하나의 element를 return해준다
⇒ hello란 class 내부에 있는 h1을 가지고 올 수 있다(id도 가능함)
첫번째 element만 가져옴
조건에 맞는 세개 다 가져오고 싶으면 querySelectorAll
⇒ 세개의 h1이 들어있는 array를 가져다 줌
querySelector("#hello); 와 getElementById("hello"); 는 같은 일을 하는 것임
하지만 후자는 하위요소 가져오는 것을 못하므로 전자만 쓸 것 같다.
const title = document.querySelector(".hello");
function handleTitleClick(){
title.style.color = "blue";
}
title.addEventListener("click", handleTitleClick);
//click하면 handleTitleClick이라는 function이 동작하길 원함
//그래서 handle~ 함수에 () 를 안넣은 것임
//즉, js가 대신 실행시켜주길 바라는 것임!