Vue/JavaScript 알고리즘
JavaScript
힛해
2023. 5. 16. 20:56
728x90
const title = document.querySelector(" .hello h1")
// .hello class 를 가진 h1의 documents를 title 에 저장해라 라는 의미입니다.
그런데 만약
< h1 class id = "hello"> grab 1 <h1>
< h1 class id = "hello"> grab 2 <h1>
< h1 class id = "hello"> grab 3 <h1>
일때
const title = document.querySelector(" .hello h1")
console.log(title); 을 하면
grab1
즉 첫번째 것만 출력된다
만약 세가지 전부 가져오고 싶다면
const title = document.querySelectorALL(" .hello h1")
만약
document.getElementById("hello"); 처럼 id를 통해 가져오고 싶다면
documet.querySelector("#hello"); 로 가져올 수 있다.