traversing dom 본문

2021 프론트 엔드 로드맵 따라가기/JS

traversing dom

알 수 없는 사용자 2021. 5. 30. 19:42
let val;

const list = document.querySelector("ul.collection");
const listItem = document.querySelector("li.collection-item:first-child");

val = listItem;
val = list;

// Get child nodes
val = list.childNodes;  // count line breaker as a text node.
val = list.childNodes[0];
val = list.childNodes[0].nodeName;
val = list.childNodes[1].nodeType;

// nodetypes
// 1 - Element
// 2 - Attribute
// 3 - Text node
// 8 - Comment
// 9 - Document itself
// 10 - Doctype

// get children element nodes
// val = list.children;  // just elements. return HTMLCollection

val = list.children;
val = list.children[1];
list.children[1].textContent = "Hello";

// Children of children
list.children[3].children[0].id = "test-id";
val = list.children[3].children[0];

// First child
val = list.firstChild;  // text node
val = list.firstElementChild; // element

// last child
val = list.lastChild;  // text node
val = list.lastElementChild; // element

// Count child elements
val = list.childElementCount;

// Get parent node
val = listItem.parentNode;
val = listItem.parentElement;
val = listItem.parentElement.parentElement;

// Get next sibling
val = listItem.nextSibling;
val = listItem.nextElementSibling.nextElementSibling.previousElementSibling;

// Get Next
// val = listItem.previousSibling;
// val = listItem.previousElementSibling;  // null

console.log(val);

'2021 프론트 엔드 로드맵 따라가기 > JS' 카테고리의 다른 글

Replace & Remove Elements  (0) 2021.05.31
Creating Elements  (0) 2021.05.31
Multiple Selector  (0) 2021.05.30
single selector  (0) 2021.05.30
Document Object  (0) 2021.05.30
Comments