HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>valila js</title> </head> <body> <div class="hello"> <h1>grab me!</h1> </div> <script src="web.js"></script> </body> </html>
const hellos = document.getElementsByClassName("hello");
console.log(hellos);
getElementsByClassName
: ClassName이 "hello"인 요소 호출
getElementsByTagName
: 태그 이름 호출
const title = document.querySelector(".hello h1");
console.log(title);
결과 :
<h1>grab me!</h1>
const title = document.querySelectorAll(".hello h1");
console.log(title);
결과 : NodeList(4) [h1, h1, h1, h1]
const title = document.querySelector(".hello h1");
title.innerText = "Hello";
grab me! -> Hello
const title = document.querySelector(".hello:first-child h1");
title.innerText = "Hello";