최대 1 분 소요

searching for elements

html의 정보 가져오기

1. getElmentById

  • id로 정보를 가져옴
  • documnet는 html을 뜻함
<h1 id="title">Hi</h1>
console.log(document.getElementById("title"));

image


  1. getElementsByClassName
    • class로 정보를 가져옴
<h1 class="hello">Hi</h1>
const hellos = document.getElementsByClassName("hello")

console.log(hellos);

image



  1. getElementsByTagName
    • tag로 정보를 가져옴
<h1 class="hello">Hi</h1>
const title = document.getElementsByTagName("h1")

console.log(title)

image



  1. querySelector
    • css방식으로 element를 가져올 수 있음
    • 첫번째 element만 가져옴
    • 여러개 가져오고 싶다면 querySelectorAll을 사용하면 됨
<h1 class="hello">Hi</h1>
const title = document.querySelector(".hello h1")

console.log(title)

image

댓글남기기