javascript-23: searching for elements
searching for elements
html의 정보 가져오기
1. getElmentById
- id로 정보를 가져옴
- documnet는 html을 뜻함
<h1 id="title">Hi</h1>
console.log(document.getElementById("title"));
- getElementsByClassName
- class로 정보를 가져옴
<h1 class="hello">Hi</h1>
const hellos = document.getElementsByClassName("hello")
console.log(hellos);
- getElementsByTagName
- tag로 정보를 가져옴
<h1 class="hello">Hi</h1>
const title = document.getElementsByTagName("h1")
console.log(title)
- querySelector
- css방식으로 element를 가져올 수 있음
- 첫번째 element만 가져옴
- 여러개 가져오고 싶다면 querySelectorAll을 사용하면 됨
<h1 class="hello">Hi</h1>
const title = document.querySelector(".hello h1")
console.log(title)
댓글남기기