반응형
DOM노드
-
nodeList 반환, childNodes 개수가 다르다?카테고리 없음 2023. 5. 21. 17:44
nodeList 반환하는 방법 html문서에서 DOM 노드에 접근하는 방법은 1. getElementsByTagName() 메소드 2. childNodes와 같은 노드 간의 관계를 이용하는 방법이 있다. 두 방법의 값으로 객체가 반환이 되는데, 이 객체는 HTML 문서와 같은 순서로 문서 내의 모든 노드를 리스트 형태로 저장하고 있다. (리스트의 노드는 인덱스 0부터 시작한다.) 첫 번째 아이템 두 번째 아이템 세 번째 아이템 1번 방법은 예상대로 3개의 li를 담은 리스트가 반환된다. 2번인 childNodes를 사용하여 반환된 노드 리스트를 보면 예상한 값과 다른 값이 반환되는 것을 확인할 수 있다. 👉 요소 노드인 li 다음에 text로 별도의 텍스트 노드가 존재함 왜 입력하지 않은 텍스트 노드가 ..