<div class="greet">Hi</div>
div
class="greet"
"Hi"
DOM의 노드 객체는 브라우저 환경에서 제공하는 호스트 객체이다.
모든 노드는 Object, EventTarget, Node 를 상속받는다.
각 노드는 특성에 따라 다른 인터페이스를 상속받기도 한다.
DOM은 HTML 문서를 계층적 구조와 정보로 표현하며, 노드 타입에 따라 필요한 기능을 DOM API로 제공한다.
id를 이용: document.getElementById(idStr)
null
반환태그 이름을 이용: Document.prototype.getElementsByTagName(tag)
클래스 이용: Document.prototype.getElementsByClassName(class)
태그, 클래스 이름을 이용한 검색은 Document.prototype
과 Element.prototype
을 이용한 검색 모두 가능하다.
CSS 선택자를 이용: Document.prototype.querySelector(selector)
null
반환SelectorAll
을 이용요소 취득여부 확인: Element.prototype.matches(selector)
HTMLCollection의 실시간 문제 해결 위한 객체이다.
non-live 객체이다.
안전하게 DOM Collection을 사용하기 위해선, 객체를 배열로 변환하여 사용할 것을 권장한다.
프로토타입 | Node | Element |
---|---|---|
텍스트 노드 포함 가능성 | O | X |
자식 노드 | childNodes | children |
첫 자식 노드 | firstChild | firstElementChild |
마지막 자식 노드 | lastChild | lastElementChild |
Node.prototype.hasChildNodes(node)
children.length
or childElementCount
Node.prototype.parentNode(selector)
프로토타입 | Node | Element |
---|---|---|
텍스트 노드 포함 가능성 | O | X |
이전 형제 노드 | previousSibling | previousElementSibling |
다음 형제 노드 | nextSibling | nextElementSibling |
Node.prototype.nodeType(Node)
: 노드 타입을 나타내는 상수를 반환Node.prototype.nodeType(Node)
: 노드 이름을 문자열로 반환Node.prototype.nodeValue
: 접근 & 할당 가능
Node.prototype.textContent
: 접근 & 할당 가능
innerText
도 비슷하나, css를 고려해야 하여 권장 XinnerHTML
: 요소 노드 안에 포함된 HTML 마크업을 문자열로 반환
`insertAdjacentHTML(위치,HTML)
기존 요소 영향X
공격에 취약
$foo.insertAdjacentHTML("beforebegin", "<p>beforebegin</p>");
$foo.insertAdjacentHTML("afterbegin", "<p>afterbegin</p>");
$foo.insertAdjacentHTML("beforeend", "<p>beforeend</p>");
$foo.insertAdjacentHTML("afterend", "<p>afterend</p>");
노드 생성
Document.prototype.createElement(tagName)
Document.prototype.createTextNode(text)
노드 삽입
Node.prototype.appendChild(childNode)
: 마지막 자식을 추가ul -> div -> li x 3
Node.prototype.insertBefore(newNode,childNode)
: childNode 앞에 newNode를 추가노드 이동
노드 복사
Node.prototype.cloneNode(isDeep)
: 노드 사본 복사노드 교체
Node.prototype.replaceChild(new,old)
: 노드 교체노드 제거
Node.prototype.removeChild(child)
: 노드 제거글로벌 어트리뷰트: id, class, style, ...
이벤트 핸들러 어트리뷰트: onClick, onChange, ...
NamedNodeMap 객체에 담겨 attributes 프로퍼티에 저장된다.
Element.prototype.getAttribute(attributeName)
로 참조가 가능하다.
Element.prototype.setAttribute(attributeNam,value)
로 변경이 가능하다.
Element.prototype.hasAttribute(attributeName)
로 존재확인이 가능하다.
Element.prototype.removeAttribute(attributeName)
로 삭제가 가능하다.
DOM 프로퍼티는 HTML 어트리뷰트를 초기값으로 가진다.
HTML 어트리뷰트: 초기 상태를 관리
DOM 프로퍼티: 최신 상태를 관리
사용자가 상태 변경 시 DOM만 반영한다.
어트리뷰트 | HTML 어트리뷰트 | DOM 프로퍼티 |
---|---|---|
유지하는 상태 | 초기 상태 | 최신 상태 |
값의 타입 | 문자열 | 여러 타입 |
data attribute
data-{name}
으로 접근 가능HTMLElement.dataset
에서 접근 가능HTMLElement.prototype.style
로 접근 & 변경 가능Element.style['background-color']
로 접근해야 함Element.style.backgroundColor
로 접근하면 됨Element.prototype.className
Element.prototype.classList
add(className)
: 클래스 이름 추가 (1개 이상)remove(className)
: 클래스 이름 제거 (1개 이상)item(index)
: index번 째의 클래스 네임 리턴contains(className)
: 존재 확인replace(old, new)
: 클래스 변경toggle(className[,force])
: 존재 O -> 제거 & 존재 X -> 추가window.getComputedStyle(element[,pseudo])
: 요소에 적용된 모든 스타일 참조가능Element vs Node vs HTMLElement
등