사용된 HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script defer src="main.js"></script>
<link rel="stylesheet" href="main.css" />
<title>JS practice</title>
</head>
<body>
<div class="btns">
<button id="id_button">ID</button>
<button class="class-button">CLASS</button>
<button class="class-button">CLASS2</button>
<button class="class-button">CLASS3</button>
</div>
</body>
</html>
1. element.children
선택된 요소의 자식요소 지정
리턴값이 HTMLCollection 배열이라 index를 통해서 접근이 가능하다.
const btns = document.querySelector(".btns");
console.log(btns.children); //HTML Collection
console.log(btns.children[1]); //자식요소중 두번째 ID
console.log(btns.children[2]); //자식요소중 세번째 CLASS
2. element. firstElementChild
선택된 요소의 첫 번째 자식요소 지정
3. element.lastElementChild
선택된 요소의 마지막 자식요소 지정
const btns = document.querySelector(".btns");
console.log(btns.firstElementChild); //첫번째 자식요소 ID
console.log(btns.lastElementChild); // 마지막 자식요소 CLASS3
4. element.parentElement
선택된 요소의 부모요소 지정
const btns = document.querySelector(".btns");
console.log(btns.parentElement); //부모 요소 지정 body
5. element previousElementSibling
선택된 요소의 형제요소 중 앞에 있는 요소
6.element.nextElementSibiling
선택된 요소의 형제요소 중 뒤에 있는 요소
const btns = document.querySelector(".btns");
console.log(btns.previousElementSibling); // null
console.log(btns.nextElementSibling); // script
const btns = document.querySelector(".btns");
console.log(btns.children);
console.log(btns.lastElementChild);
console.log(btns.firstElementChild);
console.log(btns.parentElement);
console.log(btns.previousElementSibling);
console.log(btns.nextElementSibling);
총 출력값
주요 요소 노드 프로퍼티
1. element.InnerHTML
요소 내부의 HTML 코드를 문자열로 리턴
HTML자체를 변경할 때 주로 사용된다.
const helloBtnById = document.getElementById("id_button");
helloBtnById.innerHTML = "<button>NEWID</button>";
출력값:
2. element.outerHTML
요소 노드 자체의 전체적인 HTML 코드를 문자열로 리턴
새로운 값을 할당하면 요소 자체가 바뀐다.
const helloBtnById = document.getElementById("id_button");
helloBtnById.outerHTML = "<div>NEWID</div>";
출력값:
3. element.textContent
HTML을 제외한 텍스트만을 리턴한다.
텍스트만 다루기 때문에 HTML을 바꿀수는 없다.
const helloBtnById = document.getElementById("id_button");
helloBtnById.textContent = "<div>NEWID</div>";
출력값:
새로운 요소 노드 다루기
자바스크립트에서 요소 노드를 새롭게 추가/수정/이동/삭제를 할 수 있다.
새로운 요소 만들기: document.createElement("태그이름")
요소 수정하기: element.textcontent, element.innerHTML, element.outerHTML
요소를 이동 혹은 추가하기:
1. parentElement.prepend(newElement) 자식 노드의 맨 앞에 추가
2. parentElement.append(newElement) 자식 노드의 맨 뒤에 추가
3. element.before(newElement) 요소의 앞에 추가
4. element.after(newElement) 요소의 뒤에 추가
요소 삭제하기: element.remove();
요소에 속성 추가하기: element.setAttribute("속성이름", "값")
요소의 속성 가져오기: element.getAttribute("속성이름");
요소의 속성 삭제하기: element.removeAttribute("속성이름");
class 이름 바꾸기: element.className = "바꿀값"
classList로 값 추가/삭제:
1. element.classList.add("추가할 값")
2. element.classList.remove("삭제할 값")
요소 추가하기 예시
const btns = document.querySelector(".btns");
const newButton = document.createElement("button");
newButton.textContent = "newbutton";
newButton.setAttribute("class", "class-button");
btns.append(newButton);
출력값:
'JS' 카테고리의 다른 글
[JS] 이벤트 처리하기 (0) | 2023.04.21 |
---|---|
[JS] 자바스크립트 태그 선택하기 (0) | 2023.04.20 |
[JS]모듈(Module) 모듈화(Modularization)/ Import/Export (0) | 2023.04.06 |
[JS] 다양한 배열 메소드 (0) | 2023.04.06 |
[JS] 에러 객체와 try catch문 (0) | 2023.04.05 |