//요소 노드의 attribute 프로퍼티는 요소 노드의 모든 어트리뷰트 노드의 참조가 담긴
// NamedNodeMap 객체를 반환
const { attributes } = document.getElementById('user');
// NamedNodeMap {0: id, 1:type, 2:value, id:id, type: type, vakye: value, length: 3}
Element.prototype.getAttribute/setAttribute
를 활용하면 attributes 프로퍼티를 통하지 않고 요소 노드에서 메서드를 통해 직접 HTML 어트리뷰트 값을 취득하거나 변경할 수 있음.
const inputValue = input.getAttribute('value');
input.setAttribute('value', 'foo');
Element.prototype.hasAttribute(attributeName)
: 특정 HTML어트리뷰트가 존재하는지 확인
Element.prototype.removeAttribute(attributeName)
: 특정 HTML 어트리뷰트 삭제
DOM 프로퍼티는 setter와 getter 모두 존재하는 접근자 프로퍼티다. 따라서 DOM 프로퍼티는 참조와 변경이 가능
요소 노드는 상태(state)를 가지고 있다. 2개의 상태 즉 초기 상태와 최신 상태를 관리해야함.
👉 setAttribute를 통해서 어트리뷰트 노드에서 관리하는 HTM 요소에 지정한 어트리뷰트 값 즉 초기 상태 값을 변경할 수 있음.
const input = document.getElementById('user');
// DOM 프로퍼티에 값을 할당하여 HTML 요소의 최신 상태를 변경한다.
input.value = 'foo';
console.log(input.value); // foo
// getAttribute 메서드로 취득한 HTML 어트리뷰트 값, 즉 초기 상태 값은 변하지 않고 유지된다.
console.log(input.getAttribute('value')); // ungmo2
const checkbox = document.querySelector('input[type=checkbox]');
console.log(checkbox.getAttribute('checked')); // ''
console.log(checkbox.checked); // true
HTML 요소에 정의한 사용자 정의 어트리뷰트와 자바스크립트간의 데이터 교환 가능.
data 어트리뷰트는 data-user-id, data-role과 같이 data- 접두사 다음에 임의의 이름을 붙여 사용.
HTMLElement.prototype.style
: setter와 getter 모두 존재하는 접근자 프로퍼티. 요소 노드의 인라인 스타일을 취득하거나 추가 또는 변경.
div.style.backgroundColor = 'red';
className, classList : class어트리뷰트에 대응하는 DOM 프로퍼티
Element.prototype.classList
: class 어트리뷰트의 정보를 담은 DOMTokenList 개게를 반환.
DOMTokenList 객체의 메서드 : add, remove, item, contains, replace, toggle 등이 있음.
box.classList.item(0); // box
box.classList.toggle('foo');
getComputedStyled 메서드 : HTML 요소에 적용되어 있는 모든 CSS 스타일을 참조해야 할 경우 사용.
computed style (평가된 스타일) : 요소 노드에 적용되어 있는 모든 스타일, 즉 링크 스타일, 임베딩 스타일, 인라인 스타일, 자바스크립트에서 적용한 스타일, 상속된 스타일, 기본 스타일 등 모든 스타일이 조합되어 최종적으로 적용된 스타일.
const computedStyle = window.getComputedStyle(box);
console.log(computedStyle); // CSSStyledDeclaration
// 임베딩 스타일
console.log(computedStyle.width); // 100px
// 상속 스타일 (body -> .box)
console.log(computedStyle.display); // rgb(255,0,0)
// 기본 스타일
console.log(computedStyle.display); // bock
getComputedStyled 메서드의 두 번째 인수로 :after, :before와 같은 의사 요소를 지정하는 문자열을 전달할 수 있음.
const computedStyle = window.getComputedStyle(box, ':before');
console.log(computedStyle.content);
HTML과 DOM 표준은 W3C(World Wide Web Consortium)과 WHATWC(Web Hypertext Application Technology Working Group)이라는 두 단체가 협력하며 공통된 표준을 만들고 있었음.
그런데, 2018년 4월부터 구글, 애플, 마이크로소프트, 모질라로 구성된 4개의 주류 브라우저 벤더사가 주도하는 WHATWG이 단일 표준을 내놓기로 두 단체가 합의하였음!