HTMLElement.textContent
: ์จ๊น์ฒ๋ฆฌํ ํ
์คํธ๊น์ง ๊ฐ์ ธ์ดElement.innerHTML
: ํ๋ฉด์ ๋ณด์ด๋ ํ
์คํธ๋ง ๊ฐ์ ธ์ดNode.textContent
: html ํ๊ทธ๊น์ง ๊ทธ๋๋ก ๊ฐ์ ธ์ด (์จ๊น์ฒ๋ฆฌ ์ฌ๋ถ ์๊ดโ)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;
}
em { display: none; }
html
<div id="page">
<h1><img src="./img/logo.png" alt="" /></h1>
<h2>text(html)๋ถ๋ถ๋ง ์ ๊ทผ</h2>
<ul>
<li id="one"><em>ํ ์ฝ</em> ํฌ์นธ</li>
<li id="two"><strong>์ฒญ๋ฅ</strong>์ค๋ฆฌ</li>
<li>๋ณ์๋ฆฌ</li>
<li id="three" class="orange">๋ญ</li>
<li class="sky">ํค์์</li>
<li>์ต๋ฌด์</li>
</ul>
</div>
js
const item1 = document.querySelector("#one"); // ํ ์ฝํฌ์นธ
const item2 = document.querySelector("#two"); // ์ฒญ๋ฅ์ค๋ฆฌ
const item3 = document.querySelector("#three"); // ๋ญ
const item4 = document.querySelector(".sky"); // ํค์์
const text1 = item1.textContent; // id="one"์ ํ
์คํธ ๋ถ๋ถ์ ๋ณ์์ ๋์
const text2 = item1.innerText;
console.log("textContent : ", text1); // ํ ์ฝ ํฌ์นธ
console.log("innerText : ", text2); // ํฌ์นธ
const text3 = item1.innerHTML;
const text4 = item2.innerHTML;
console.log(text3); // <em>ํ ์ฝ</em> ํฌ์นธ
console.log(text4); // <strong>์ฒญ๋ฅ</strong>์ค๋ฆฌ
const item3Txt = item3.innerText;
item3.innerHTML = `<a href="https://terms.naver.com/entry.naver?docId=742195&cid=46681&categoryId=46681"> ${item3Txt} </a>`;
const item4Txt = item4.innerText;
item4.innerHTML = `<a href="https://terms.naver.com/entry.naver?docId=1151737&cid=40942&categoryId=32594"> ${item4Txt} </a>`;
createElement(a)
: ๋ฌธ์ ๊ฐ์ฒด(html tag) a๋ฅผ ์์ฑ
createTextNode(a)
: ์์ฑ๋ ํ๊ทธ ๋ด๋ถ์ ๋ค์ด๊ฐ ํ
์คํธ a ์์ฑ
Node.appendChild(a)
: Node(๋ถ๋ชจโญ) ์ a Node๋ฅผ ์ฝ์
ํ์ฌ ์์ ์์๋ฅผ ๋ง๋ฆ (์ฝ์
ํ ํ๊ทธ์ ์ ์ผ ๋ ๋ถ๋ถ)
Node.append(a)
: ์๋ ๋์ผ
Node.prepend(a)
: Node(๋ถ๋ชจโญ) ์ a Node๋ฅผ ์ฝ์
ํ์ฌ ์์ ์์๋ฅผ ๋ง๋ฆ (์ฝ์
ํ ํ๊ทธ์ ๋งจ ์ ๋ถ๋ถ)
Node.insertBefore(a, b)
: ๋ถ๋ชจ Node์ ์์ ์์ b์์ a๋ฅผ ์ฝ์
Node.after(a)
: Node(๋ถ๋ชจโ) ๋ค์ a Node๋ฅผ ์ฝ์
(Node๋ ๊ฐ์ ํ์ )
Node.before(a)
: Node(๋ถ๋ชจโ) ์์ a Node๋ฅผ ์ฝ์
(Node๋ ๊ฐ์ ํ์ )
Node.removeChild(a)
: 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>DOM ์์ ์ ๊ฑฐ ์ถ๊ฐ</h2>
<ul>
<li>1. ํฌ์นธ</li>
<li>2. ์ค๋ฆฌ</li>
<li>3. ๋ณ์๋ฆฌ</li>
<li>4. ๋ญ</li>
<li>5. ํค์์</li>
<li>6. ์ต๋ฌด์</li>
</ul>
<!-- <ul>
<li>๋ ๋ฒ์จฐ ul์
๋๋ค</li>
</ul> -->
</div>
js
const parentElement = document.getElementsByTagName("ul")[0];
const removeElement = document.getElementsByTagName("li")[2];
parentElement.removeChild(removeElement); // ์ ๊ฑฐ
const newElement = document.createElement("li"); // ๋ฌธ์๊ฐ์ฒด(html tag) ์์ฑ
const newText = document.createTextNode("์ ์ข
๋ฅ(js๋ก ์ถ๊ฐ)");
newElement.appendChild(newText);
parentElement.append(newElement); // li ๋งจ ๋ง์ง๋ง์ ์ถ๊ฐ๋จ
parentElement.append("<li>์ฌ์ฝ๋ด์ด</li>"); // ๋จ์ํ๊ฒ ํ
์คํธ๋ก ์ธ์
const h2 = document.querySelector("h2");
const elH3 = document.createElement("h3");
elH3.textContent = "small title";
// h2.after(elH3);
// h2.before(elH3);
page.insertBefore(elH3, h2);
console.log(elH3);
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; }
#result {
background: #90b460;
margin: 20px;
padding: 20px;
border-radius: 10px;
}
html
<div id="page">
<h1><img src="./img/kiwi_icon.png" alt="" /></h1>
<h2>Attribute ๊ฒ์ฌ</h2>
<ul>
<li id="one" class="toucan">ํฌ์นธ</li>
<li>๋ญ</li>
<li>ํค์์</li>
<li id="four">์ต๋ฌด์</li>
</ul>
<div id="result"></div>
</div>
js
const item1 = document.getElementById("one");
const pos = document.getElementById("result");
if (item1.hasAttribute("class")) {
const attValue = item1.getAttribute("class");
console.log("class ์์ฑ์ ๊ฐ์? : ", attValue);
pos.innerHTML = `์ฒซ ๋ฒ์งธ item์ class ์ด๋ฆ์? <br> <big>${attValue}</big> ์
๋๋ค.`;
}
Element.hasAttribute(a)
: ์์์ a ์์ฑ์ ๊ฐ์ง๊ณ ์๋์ง ์์๋ด(boolen๊ฐ์ผ๋ก ๋ฐํ)Element.setAttribute(name, value)
: ์์์ ์์ฑ, ์์ฑ๊ฐ์ ๋์
ํ์ฌ ๋ง๋ค์ด์ค (์ถ๊ฐ)Element.removeAttribute(name)
: ์์์ ์๋ ์์ฑb๋ฅผ ์ ๊ฑฐElement.getAttribute(a)
: ์์์ ์๋ a์์ฑ์ ์์ฑ๊ฐ(value)์ ๊ฐ์ ธ์ด(๋์
)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; }
#result {
background: #90b460;
margin: 20px;
padding: 20px;
border-radius: 10px;
}
html
<div id="page">
<h1><img src="./img/kiwi_icon.png" alt="" /></h1>
<h2>Attribute ์์ฑ, ๋ณ๊ฒฝ</h2>
<ul>
<li id="one" class="toucan">ํฌ์นธ</li>
<li>๋ญ</li>
<li title="kiwi">ํค์์</li>
<li id="four"><a>์ต๋ฌด์</a></li>
</ul>
<div id="result"></div>
</div>
js
const item1 = document.getElementById("one");
const pos = document.getElementById("result");
// ํด๋์ค ์ถ๊ฐ
item1.classList.add("line"); // ํด๋์ค ์ถ๊ฐ // class="toucan" -> "toucan line"
item1.classList.replace("toucan", "toco"); // ํด๋์ค์ value๋ฅผ ๋ณ๊ฒฝ (class="toucan line" -> "toco line")
// aํ๊ทธ์ attribute ์ถ๊ฐ
const item2 = document.querySelector("#four a");
item2.setAttribute("href", "https://terms.naver.com/entry.naver?docId=1164605&cid=40942&categoryId=32617"); // ์์ฑ ์ถ๊ฐ (a ํ๊ทธ์ ๋งํฌ ์ถ๊ฐ)
item2.setAttribute("target", "_blank"); //์์ฑ ์ถ๊ฐ ๊ฐ๋ฅ. // ์ ํญ์ผ๋ก ์ด๋ฆผ
const item3 = document.querySelectorAll("li").item(1);
item3.setAttribute("class", "red"); // ์ธ์ 2๊ฐ ํ์
const item4 = document.querySelectorAll("li").item(2);
item4.removeAttribute("title"); // ์์ฑ ์ญ์
console.log(item4);
const item3Name = item3.getAttribute("class");
pos.innerHTML = `๋ ๋ฒ์งธ li ์ class์ value๋ <br> ${item3Name} ์ด๋ค.`;
}