자바스크립트 1o1 - DOM으로 작업하기 - 1
9033 단어 javascript1o1javascript
This article was originally posted in my Hashnode Blog
DOM
는 D
ocument O
bject M
odel을 나타냅니다.DOM
는 문서에 액세스하기 위한 표준입니다.프로그램이나 스크립트가 문서의 내용, 스타일 및 구조를 동적으로 변경할 수 있도록 하는 언어 중립적 플랫폼입니다.
이 기사에서 우리는 배울 것입니다
문서에 있는 요소의 모델 개체에 액세스하는 방법.
<p id="demo"></p>
<script>
let paragraph = document.getElementById("demo");
//or
let paragraph = document.querySelector("#demo");
</script>
#
is used before the id to denote we are selecting the elements having id while usingquerySelector
document.getElementById
는 id
가 있는 요소를 가져오는 데만 사용되지만 document.querySelector
& document.querySelectorAll
는 다른 속성을 가진 요소를 가져오는 데도 사용할 수 있습니다.<p class='para'> First Paragraph </p>
<p class='para'> Second Paragraph </p>
<script>
let paragraphs = document.getElementsByClassName("para"); //returns HTMLCollection
//or
let paragraphs = document.querySelectorAll('.para'); //returns NodeList
</script>
.
is used before the class to denote we are selecting the elements having class while usingquerySelector
orquerySelectorAll
여기서는
querySelectorAll
대신 querySelector
를 사용했습니다.이는
querySelectorAll
가 인수에 지정된 선택기와 일치하는 모든 요소를 반환하기 때문입니다. 그러나 querySelector
는 선택기와 일치하는 문서의 유일한 첫 번째 요소를 반환합니다.예를 들어
<p class='para'> First Paragraph </p>
<p class='para'> Second Paragraph </p>
<script>
let paragraph = document.querySelector('.para')
// returns the paragraph with text "First Paragraph"
</script>
따라서 Id로 선택할 때만
querySelector
를 사용합니다. 그렇지 않으면 querySelectorAll
를 사용합니다.There is more to it with selecting elements with class
예를 들어
<p class='block'> This is paragraph </p>
<p class='block'> This is paragraph 2 </p>
<h5 class='block'> This is heading </h5>
<script>
let elems = document.querySelectorAll('p.block');
//returns only paragraph with class 'block'
</script>
<p> Hi </p>
<p> How are you ? </p>
<script>
let elems = document.getElementsByTagName("p"); // returns HTMLCollection
//or
let elems = document.querySelectorAll("p"); // returns Nodelist
</script>
Not using any notation before the argument denotes selecting elements with tag name in
querySelector
orquerySelectorAll
<h1 title='heading'> Working with Dom </h1>
<p title='heading'> Attribute Selector </p>
<script>
let elems = document.querySelectorAll("h1[title='heading']");
// returns NodeList of h1 elements with title 'heading'
let elems = document.querySelectorAll("[title='heading']");
// returns NodeList of all elements with title 'headiing'
</script>
결론
.
는 클래스용입니다. #
는 id용이고 [ attributeName = "value"]
는 속성용입니다. 요소를 선택하기 위한 선택자입니다. querySelectorAll
는 요소의 NodeList
를 반환하고, getElementsByClassName
, getElementsByTagName
는 HTMLCollection
를 반환하고 getElementById
& querySelector
는 요소 자체를 반환합니다. querySelector
또는 querySelectorAll
를 사용할 때 요소 유형을 지정할 수도 있습니다. 눈치채셨을 수도 있습니다. 위의 코드에서
NodeList
와 HTMLCollection
에 대해 많이 언급했습니다. 다음 기사에서 이 낯선 사람들에 대해 알아볼 것입니다.
Reference
이 문제에 관하여(자바스크립트 1o1 - DOM으로 작업하기 - 1), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/anjalbinayak/working-with-dom-1-1b7h텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)