230316 - DOM

๋ฐฑ์Šน์—ฐยท2023๋…„ 3์›” 16์ผ
1

๋‚ด์ผ์ˆ˜์ •~

๐Ÿšฉ Document method

querySelectorAll()

๐Ÿ“ ์„ค๋ช…

  • querySelector()์™€ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๋™์ผ
  • ์„ ํƒ์ž๋ฅผ ์„ ํƒํ•˜์—ฌ ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•œ ๊ฐ์ฒด์ธ nodeList๋ฅผ ๋ฐ˜ํ™˜


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์˜ˆ์ œ

css

* { margin: 0; padding: 0; }
body { background: #BDCDD6; }
body > div {
  width: 400px;
  margin: 150px auto;
  padding: 2px;
  text-align: center;
  border: 1px solid #DF7857;
}
h1 { margin: 10px 0; }
h1 img { width: 150px; }
h2 { color:#737db6; margin-bottom: 30px; }
li {
  list-style: none;
  background: #6096B4;
  color: #fff;
  line-height: 50px;
  margin: 2px;
  padding-left: 20px;
}
.red { background: #E7AB9A; }
.orange { background: #f3be70; }
.sky { background: #93BFCF; }
.line { text-decoration: line-through; }

html

<div id="page">
  <h1><img src="./img/kiwi_icon.png" alt="" /></h1>
  <h2>๋ชจ๋“  ์„ ํƒ์ด ๊ฐ€๋Šฅ</h2>
  <ul>
    <li>ํˆฌ์นธ</li>
    <li>์˜ค๋ฆฌ</li>
    <li class="red">๋ณ‘์•„๋ฆฌ</li>
    <li class="red">๋‹ญ</li>
    <li class="red">ํ‚ค์œ„์ƒˆ</li>
    <li class="red">์•ต๋ฌด์ƒˆ</li>
  </ul>
</div>

js

const el1 = document.querySelector("li.red");
el1.classList.add("line");
// querySelector() - ๋ชจ๋“  ์„ ํƒ ๊ฐ€๋Šฅ(tag, id, class)
// ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์„๋•Œ๋Š” ์ฒซ๋ฒˆ์งธ ๊ฒƒ๋งŒ ์„ ํƒ

const el2 = document.querySelectorAll("li.red");
console.log(el2);
el2[1].style.backgroundColor = "yellowgreen";

for (let i = 0; i <= el2.length; i++) { // el2์˜ ๊ธธ์ด๋งŒํผ = class="red"์˜ ๊ฐฏ์ˆ˜๋งŒํผ = 3
  el2[i].style.fontWeight = "bold";
  el2[i].style.color = "yellow";
  // el2[i].style.cssText = "font-weight: bold"; 
}
// style.cssText - css์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์†์„ฑ๋ช…์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ. ๋Œ€์‹  ๊ธฐ์กด์— ์ ์šฉ๋˜์–ด ์žˆ๋˜ ๋‹ค๋ฅธ css๊ฐ€ ์ „๋ถ€ ์ง€์›Œ์ง€๊ณ  ํ•ด๋‹น style๋งŒ ๋‚จ์Œ

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






๐Ÿšฉ ํ˜•์ œ๊ด€๊ณ„ ์„ ํƒ์ž

Sibling

๐Ÿ“ ์„ค๋ช…

  • ํ•ด๋‹น node์˜ ํ˜•์ œ node๋ฅผ ๊ฐ€๋ฆฌํ‚ด

์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ
  • Node.previousSibling : Node์˜ ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๋…ธ๋“œ ๋ฐ˜ํ™˜
  • .nextSibling : Node์˜ ๋ฐ”๋กœ ๋’ค์— ์žˆ๋Š” ๋…ธ๋“œ ๋ฐ˜ํ™˜


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์˜ˆ์ œ

css

* { margin: 0; padding: 0; }
body { background: #BDCDD6; }
body > div {
  width: 400px;
  margin: 150px auto;
  padding: 2px;
  text-align: center;
  border: 1px solid #DF7857;
}
h1 { margin: 10px 0; }
h1 img { width: 150px; }
h2 { color:#737db6; margin-bottom: 30px; }
li {
  list-style: none;
  background: #6096B4;
  color: #fff;
  line-height: 50px;
  margin: 2px;
  padding-left: 20px;
}
.red { background: #E7AB9A; }
.orange { background: #f3be70; }
.sky { background: #93BFCF; }
.line { text-decoration: line-through; }

html

<div id="page">
  <h1><img src="./img/kiwi_icon.png" alt="" /></h1>
  <h2>ํ˜•์ œ๊ด€๊ณ„ ์„ ํƒ์ž</h2>
  <ul>
    <li>ํˆฌ์นธ</li><li>์˜ค๋ฆฌ</li><li>๋ณ‘์•„๋ฆฌ</li><li id="three">๋‹ญ-START</li><li>ํ‚ค์œ„์ƒˆ</li><li>์•ต๋ฌด์ƒˆ</li>
  </ul>
</div>

js

const elStart = document.getElementById("three");
const elPrev = elStart.previousSibling; // elStart์˜ ํ•˜๋‚˜ ์•ž์— ์žˆ๋Š” ์„ ํƒ์ž
const elNext = elStart.nextSibling; // elStart์˜ ๋ฐ”๋กœ ๋‹ค์Œ์— ์žˆ๋Š” ์„ ํƒ์ž

elPrev.className = "red";
elNext.classList.add("orange"); // enter ์น˜๋ฉด ์ ์šฉโŒ, ํ•œ ์ค„๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ์ ์šฉโญ•

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






๐Ÿšฉ childNodes

childNodes, children

๐Ÿ“ ์„ค๋ช…



โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์˜ˆ์ œ

css

* { margin: 0; padding: 0; }
body { background: #BDCDD6; }
body > div {
  width: 400px;
  margin: 150px auto;
  padding: 2px;
  text-align: center;
  border: 1px solid #DF7857;
}
h1 { margin: 10px 0; }
h1 img { width: 150px; }
h2 { color:#737db6; margin-bottom: 30px; }
li {
  list-style: none;
  background: #6096B4;
  color: #fff;
  line-height: 50px;
  margin: 2px;
  padding-left: 20px;
}
.red { background: #E7AB9A; }
.orange { background: #f3be70; }
.sky { background: #93BFCF; }
.purple { background: #d49dd4; color: yellow; }
.line { text-decoration: line-through; }
.check {
  background: #fff url("../img/check-mark.png") no-repeat 90% 50% / 25px;
  color: red;
}

html

<div id="page">
  <h1><img src="./img/kiwi_icon.png" alt="" /></h1>
  <h2>childNodes, children<span>ใ…Žใ…‡</span></h2>
  <ul>
    <!-- ์ฃผ์„์ž…๋‹ˆ๋‹ค -->
    <li>ํˆฌ์นธ</li>
    <li>์˜ค๋ฆฌ</li>
    <li>๋ณ‘์•„๋ฆฌ</li>
    <li>๋‹ญ</li>
    <li>ํ‚ค์œ„์ƒˆ</li>
    <li>์•ต๋ฌด์ƒˆ</li>
  </ul>
  <ul><li
    >๋‘ ๋ฒˆ์งธ ul ์‹œ์ž‘ - ํˆฌ์นธ</li
    ><li>์˜ค๋ฆฌ</li
    ><li class="red">๋ณ‘์•„๋ฆฌ</li
    ><li class="sky">๋‹ญ</li
    ><li>ํ‚ค์œ„์ƒˆ</li
    ><li>์•ต๋ฌด์ƒˆ</li
  ></ul>
</div>

js

const elStart = document.getElementsByTagName("ul")[1];
// getElementsByTagName() - ํ•ญ์ƒ ๋ฐฐ์—ด๋กœ ์ธ์‹
// ๋ฐฐ์—ด์˜ index๋ฅผ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํƒœ๊ทธ์˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ด

const elFirst = elStart.firstChild; // ๋‘ ๋ฒˆ์งธ ul ์‹œ์ž‘ - ํˆฌ์นธ
const elLast = elStart.lastChild; // ์•ต๋ฌด์ƒˆ
const elThird = elStart.childNodes[2]; // ๋ณ‘์•„๋ฆฌ
console.log("elStart.childNodes๋Š”?", elStart.childNodes);

elFirst.classList.add("check");
elLast.classList.add("orange");
elThird.classList.add("line");
elThird.classList.replace("red", "purple");

/*
  A.firstChild - A์˜ ์ž์‹ ๋…ธ๋“œ๋“ค ์ค‘ ์ฒซ ๋ฒˆ์งธ
  A.lastChild - A์˜ ์ž์‹ ๋…ธ๋“œ๋“ค ์ค‘ ๋งˆ์ง€๋ง‰
  A.childNodes[n] - A์˜ ์ž์‹ ๋…ธ๋“œ๋“ค ์ค‘ n ๋ฒˆ์งธ
  A.classList.replace("b", "c") - A์˜ ํด๋ž˜์Šค b๋ฅผ c๋กœ ๋ฐ”๊พผ๋‹ค
*/

const elStart2 = document.getElementsByTagName("ul")[0];
const elChildren = elStart2.children; 
console.log("children์€?", elChildren);

const elCh2 = elStart2.children[1];
elCh2.classList = "orange";

// const elFirstChild = elStart2.firstChild;
// console.log("elFirstChild", elFirstChild); // li๋งŒ ์ธ์‹ํ•˜์ง€ ์•Š์Œ
// elFirstChild.classList.add("check"); //children ๊ณผ ๋‹ค๋ฆ„ Node

console.log("elStart2(์ฒซ ๋ฒˆ์งธ ul)์˜ ์ž์‹ ๊ฐฏ์ˆ˜๋Š”?", elStart2.childNodes.length);
console.log("elStart2(์ฒซ ๋ฒˆ์งธ ul)์˜ ์ž์‹ ๊ฐฏ์ˆ˜๋Š”?", elStart2.children.length);

const elPa = elStart2.parentNode;
console.log("elPa", elPa);

const h2 = document.getElementsByTagName("h2")[0];
console.log("h2์— ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”๊ฐ€?", h2.hasChildNodes);
console.log("h2์— ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”๊ฐ€?", h2.hasChildNodes()); // ์†Œ๊ด„ํ˜ธ ์ž‘์„ฑ ์‹œ boolean๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜

/*
  A.childNodes - A์˜ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ ๋ฐ˜ํ™˜(์—”ํ„ฐ, ์—ฌ๋ฐฑ, ์ฃผ์„ ๋“ฑ์„ ํฌํ•จ)
  A.children - A์˜ ๋ชจ๋“  ์ž์‹์š”์†Œ ๋ฐ˜ํ™˜(์—”ํ„ฐ, ์—ฌ๋ฐฑ, ์ฃผ์„ ๋“ฑ ์ œ์™ธ)
  A.parentNode - A์˜ ๋ถ€๋ชจ ๋…ธ๋“œ
  A.hasChildNodes() - A์˜ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„๋ด„
*/

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






๐Ÿšฉ childNodes

childNodes, children

๐Ÿ“ ์„ค๋ช…



โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์˜ˆ์ œ

css

* { margin: 0; padding: 0; }
body { background: #BDCDD6; }
body > div {
  width: 400px;
  margin: 150px auto;
  padding: 2px;
  text-align: center;
  border: 1px solid #DF7857;
}
h1 { margin: 10px 0; }
h1 img { width: 150px; }
h2 { color:#737db6; margin-bottom: 30px; }
li {
  list-style: none;
  background: #6096B4;
  color: #fff;
  line-height: 50px;
  margin: 2px;
  padding-left: 20px;
}
.red { background: #E7AB9A; }
.sky { background: #93BFCF; }
}

html

<div id="page">
  <h1><img src="./img/logo.png" alt="" /></h1>
  <h2>nodeValue</h2>
  <ul>
    <li>ํˆฌ์นธ</li>
    <li id="two">์˜ค๋ฆฌ</li>
    <li>๋ณ‘์•„๋ฆฌ</li>
    <li id="four" class="red">๋‹ญ</li>
    <li class="sky">ํ‚ค์œ„์ƒˆ</li>
    <li>์•ต๋ฌด์ƒˆ</li>
  </ul>
</div>

js

let elText1 = two.nodeValue;
// let elText2 = two.children.nodeValue; // console.log(elText2); - undefined
let elText2 = two.firstChild.nodeValue; // ํ…์ŠคํŠธ ์š”์†Œ๋„ ์ž์‹ ๋…ธ๋“œ๋กœ ์ธ์‹
console.log(elText1); // null
console.log(elText2); // ์˜ค๋ฆฌ.  // ํ…์ŠคํŠธ ์š”์†Œ๋„ ์ž์‹ ๋…ธ๋“œ๋กœ ์ธ์‹

two.firstChild.nodeValue = elText2.replace("์˜ค๋ฆฌ", "๋ถ€์—‰์ด");

let elText3 = four.firstChild.nodeValue;
// four.firstChild.nodeValue = elText3.replace("๋‹ญ", "์˜ค๋ชฉ๋ˆˆ์ด");
four.firstChild.nodeValue = "์˜ฌ๋นผ๋ฏธ"; // text ๋ถ€๋ถ„์„ ํ†ต์งธ๋กœ ๋ณ€๊ฒฝํ•จ ๊ณต๋ฐฑ์œผ๋กœ ๋†”๋‘๋ฉด ํ…์ŠคํŠธ ๋‚ด์šฉ์ด ์‚ฌ๋ผ์ง 

/*
        A.nodeValue - A์˜ ๋…ธ๋“œ๊ฐ’ (ํ…์ŠคํŠธ ์š”์†Œ)
        A.replace("b", "c"); - ๋ฌธ์ž์—ด A์— ์žˆ๋Š” b๋ฅผ c๋กœ ๋ฐ”๊พผ๋‹ค
      */

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€