Element 객체는 엘리먼트를 추상화한 객체다. HTMLElement 객체와의 관계를 이해하기 위해서는 DOM의 취지에 대한 이해가 선행되야 한다. DOM은 HTML만을 제어하기 위한 모델이 아니다. HTML이나 XML, SVG, XUL과 같이 마크업 형태의 언어를 제어하기 위한 규격이기 때문에 Element는 마크업 언어의 일반적인 규격에 대한 속성을 정의하고 있고, 각각의 구체적인 언어(HTML,XML,SVG)를 위한 기능은 HTMLElement, SVGElement, XULElement와 같은 객체를 통해서 추가해서 사용하고 있다.
다른 객체들과의 관계
DOM의 계층구조에서 Element 객체의 위치는 아래와 같다.
주요기능
식별자
문서내에서 특정한 엘리먼트를 식별하기 위한 용도로 사용되는 API
- lement.classList
- Element.className
- Element.id
- Element.tagName
조회
엘리먼트의 하위 엘리먼트를 조회하는 API
- Element.getElementsByClassName
- Element.getElementsByTagName
- Element.querySelector
- Element.querySelectorAll 속성
엘리먼트의 속성을 알아내고 변경하는 API
- Element.getAttribute(name)
- Element.setAttribute(name, value)
- Element.hasAttribute(name);
- Element.removeAttribute(name);
실습
getElementById
body>
<ul>
<li>html</li>
<li>css</li>
<li id="active" class = "important current">javascript</li>
</ul>
<script>
console.log(document.getElementById('active').tagName)
</script>
</body>
className
<body>
<ul>
<li>html</li>
<li>css</li>
<li id="active" class = "important current readed">javascript</li>
</ul>
<script>
let active = document.getElementById('active')
active.className = "important current"
console.log(active.className)
active.className += " readed"
</script>
</body>
classList
<body>
<ul>
<li>html</li>
<li>css</li>
<li id="active" class = "mark trou">javascript</li>
</ul>
<script>
let active = document.getElementById('active')
active.classList //DOMTokenList 라는 유사 배열에 저장된다.
for(let i = 0; i<active.classList.length; i++){
console.log(active.classList[i])
}
active.classList.add('important')
active.classList.remove('important')
active.className.toggle('important') //넣자마자 사라짐.
</script>
</body>
조회
<body>
<ul>
<li class = "marked">html</li>
<li>css</li>
<li id = "active">Javascript
<ul>
<li>Javascript</li>
<li class = "marked">DOM</li>
<li class = "marked">BOM</li>
</ul>
</li>
</ul>
<script>
let list = document.getElementsByClassName('marked')
console.group('document')
for(let i = 0; i < list.length; i++){
console.log(list[i].textContent)
}
console.groupEnd()
console.group('active')
let active = document.getElementById('active')
let list = active.getElementsByClassName('marked')
for(let i = 0; i<list.length;i++){
console.log(list[i].textContent)
}
console.groupEnd()
</script>
</body>
document 객체는 문서 전체를 의미하는 엘리먼트이기 때문에 document의 조회 메서드는 문서 전체를 대상으로 엘리먼트를 조회한다.
Element 객체 역시도 getElementsBy 엘리먼트를 가지고 있는데 Element 객체의 조회 메소드는 해당 엘리먼트의 하위 엘리먼트를 대상으로 조회를 수행한다,
속성API
body>
<a id = "target" href = "https://www.naver.com">naver</a>
<script>
let t = document.getElementById('target')
console.log(t.getAttribute('href')) // 출력 값 https://www.naver.com
t.setAttribute('title', 'daum.net') // a태그에 title과 daum.net 삽입
console.log(t.hasAttribute('title')) // title 속성을 가지고 있는지 출력 -> True
t.removeAttribute('title') // title속성 삭제
t.hasAttribute('title') //False
t.setAttribute('title', 'daum.net')
t.hasAttribute('title') //True
</script>
</body>
속성과 프로퍼티
<body>
<p id = "target">
Hello World
</p>
<script>
let target = document.getElementByIdI('target')
//attribute 방식
target.setAttribute('class', 'important')
//property 방식
target.className = 'important'
</script>
</body>
class | className |
readonly | readOnly |
rowspan | rowSpan |
colspan | colSpan |
usemap | useMap |
frameborder | frameBorder |
for | htmlFor |
maxlength | maxLength |
SetAttribute('class' , 'important') 와 className = 'important'는 같은 결과를 만든다. 하지만 전자는 attribute 방식(이하 속성)이고 후자는 property(이하 프로퍼티)방식이다. 프로퍼티 방식은 좀 더 간편하고 속도도 빠르지만 실제 html속성의 이름과 다른 이름을 갖는 경우가 있다. 그것은 자바스크립트의 이름 규칙때문이다. 심지어 속성과 프로퍼티 값이 다를수도 있다.
'개발' 카테고리의 다른 글
[Javascript] 문서의 기하학적 특성 (0) | 2020.12.23 |
---|---|
[Javascript] Node 객체 (0) | 2020.12.22 |
CSS 공부 - 5 (0) | 2020.12.10 |
CSS 공부 - 4 (0) | 2020.12.09 |
CSS 공부 - 3 (0) | 2020.12.08 |