์ปจํ ์ธ ์ ๋ด์ฉ์ 'Codeit | ์ธํฐ๋ํฐ๋ธ ์๋ฐ์คํฌ๋ฆฝํธ' ์จ๋ผ์ธ ๊ฐ์ข๋ฅผ ๋ฃ๊ณ ํ์ตํ ๋ด์ฉ๊ณผ ์ฝ๋๋ค์ ๋จธ๋ฆฟ์์ ์ ๋ฆฌํ๊ธฐ ์ํด ์ ๋ฆฌํด ๋ณธ ๋ด์ฉ์ ๋๋ค.(ํ์ฌ๋ผ๋ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ๋ฐ๋ก ์ปจํ ์ธ ๋ฅผ ์ญ์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค.)
์น์์์ ์๋ฐ์คํฌ๋ฆฝํธ๋ ์ด๋ป๊ฒ ๋์ํ๋์ง์ ๋ํด ์์๋ณด์.
์ฒซ๋ฒ์งธ๋ HTML์์์ ์๋ฐ์คํฌ๋ฆฝํธ ๋์
๋๋ฒ์งธ๋ CSS์์์ ์๋ฐ์คํฌ๋ฆฝํธ ๋์
์ธ๋ฒ์งธ๋ Browser์์์ ์๋ฐ์คํฌ๋ฆฝํธ ๋์
์ด 3๊ฐ์ง์ ์๋ฐ์คํฌ๋ฆฝํธ ๋์์ค ์ฒซ๋ฒ์งธ์ธ HTML์์์ ์๋ฐ์คํฌ๋ฆฝํธ ๋์์ ๊ฐ๋จํ ์์ ๋ฅผ ํตํด์ ์์๋ณด์.
cssํ์ผ์ ๋ฐ๋ก ์์ฑํ๊ธฐ ๊ท์ฐฎ์์ tailwind css์ ๋์์ ๋ฐ์๋ค.
<!-- head๋ถ๋ถ์ link ์ถ๊ฐ -->
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
<body class="p-20">
<h1 id = "title-text" class="text-xl font-bold"> Interactive JavaScript-HTML </h1>
<div class="container">
<div id="count" class="text-xl font-bold mb-8">0</div>
<button id="plusBtn" class="bg-gray-400 rounded shadow p-2 hover:bg-gray-200">+</button>
<button id="minusBtn" class="bg-gray-400 rounded shadow p-2 hover:bg-gray-300">-</button>
<div class="mt-5 flex gap-5">
<button
class="colorBtn p-5 shadow rounded bg-red-600 hover:bg-red-400"
data-color="#dc2626"
>
red
</button>
<button
class="colorBtn p-5 shadow rounded bg-yellow-600 hover:bg-yellow-400"
data-color="#dc7706"
>
orange
</button>
<button
class="colorBtn p-5 shadow rounded bg-blue-600 hover:bg-blue-400"
data-color="#2563eb"
>
blue
</button>
<button
class="colorBtn p-5 shadow rounded bg-purple-600 hover:bg-purple-400"
data-color="#7c3aed"
>
purple
</button>
</div>
</div>
<script src="js/index.js"></script>
</body>
index.js ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ํตํด ์๋์ 2๊ฐ์ง ๊ธฐ๋ฅ์ ๊ตฌํํด์ผ ํ๋ค.
์ฒซ๋ฒ์งธ. +๋ฒํผ์ ๋๋ฅด๋ฉด id=count 0์ ์ซ์๊ฐ 1์ฉ ์ฆ๊ฐํ๊ณ , -๋ฒํผ์ ๋๋ฅด๋ฉด ์ซ์๊ฐ -1์ฉ ๊ฐ์ํ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์์ DOM์์๋ฅผ ์ ๊ทผํ๊ธฐ ์ํด์๋ ์ง์ํ๋ ๋ฉ์๋๋ฅผ ํ์ฉํด์ผ ํ๋ค.
โ getElementById ๋ฉ์๋๋ฅผ ํ์ฉํด count,plusBtn,minusBtn ์๋ฆฌ๋จผํธ์ ์ ๊ทผํด ์๋ฐ์คํฌ๋ฆฝํธ ๋ณ์์ ๋ด์๋ณด์.
const countTag = document.getElementById('count');
const increaseTag = document.getElementById('plusBtn');
const decreaseTag = document.getElementById('minusBtn');
console.log(countTag);//outputs: <div id="myNumber" class="text-xl font-bold mb-8">0</div>
๋ฌธ๋ getElementById('')๋ก ๊ฐ์ ธ์จ ๋ฐ์ดํฐ ํ์ ์ด ๋ฌด์์ธ์ง ๊ถ๊ธํด์ ํ์ธํด๋ณด๋
console.log(typeof countTag)
ouput์ object ์๋ค.console.log(countTag)
์ถ๋ ฅ๊ฐ์ด ํ๋์ div id๊ฐ์ ๊ฐ์ ธ์์ง๋ง id๊ฐ ๋ด๋ถ์ ์ฌ๋ฌ์์๋ค์ด ์๋ค๋ฉด ์์๋ค๊ฐ๋ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
โก countTag์ ๋ด๊ฒจ์๋ 0์ ๊ฐ์ ์ซ์๊ฐ์ผ๋ก ๋ฐ๊ฟ์ ๋ณ์์ ๋ด์์ค์ผ ํ๋ค.
const jjuCount = +countTag.textContent;
โ
countTag์์ '+'๊ฐ ๋ถ์ ์ด์ : ์ฌ๊ธฐ์์ +๋ ์ฐ์ฐ์์ +๊ฐ ์๋๋ค. textContent ํ๋กํผํฐ๋ ํด๋น id๊ฐ์ ๋ด์ฉ๋ค์ stringํํ๋ก ๊ฐ์ ธ์จ๋ค. <div id=count>0</div>
์ฆ '0'์ด๋ผ๋ ๊ฐ์ ๋ฌธ์์ด๋ก ๊ฐ์ ๊ฐ์ ธ์ค๊ฒ ๋๋๋ฐ ์ฌ๊ธฐ ์์ +๋ฅผ ๋ถ์ด๊ฒ ๋๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ์์๋ ์ซ์ํ์ผ๋ก ํ๋ณํ์ด ๋๋ค.๋ณ์์์ '+'๊ฐ ์๋ Number(countTage.textContent)
๋ฅผ ์ฌ์ฉํด๋ ๋๋ค.
โ textContent ์ ๊ฐ์ ์ญํ ์ ํ๋ innerText๋ผ๋ ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํด๋ ๋๋ค.
- textContent& innerText๊ณตํต์ : ํ๊ทธ์ ๊ฐ์ ๋ฌธ์์ด๋ก ๊ฐ์ ธ์จ๋ค๋ ๊ฒ
- textContent& innerText์ฐจ์ด์
- ์์ชฝ ๋์ ๋์ด์ฐ๊ธฐ ๊ณต๋ฐฑ๊ณผ ์ค๊ฐ์ ๋์นธ ์ด์์ ๋์ด์ฐ๊ธฐ ๊ณต๋ฐฑ ์ํ ๊ทธ๋๋ก ๋ฌธ์์ด์ ๊ฐ์ ธ์ค๋ ๊ฑด 'textContent' ํ๋กํผํฐ
- 2์นธ ์ด์์ ๋์ด์ฐ๊ธฐ ๊ณต๋ฐฑ์ ์ ์ธํ๊ณ ๋ฌธ์์ด๊ฐ์ ๊ฐ์ ธ์ค๋ ๊ฑด 'innerText'ํ๋กํผํฐ
<h1 id = "title-text" class="text-xl font-bold"> Interactive JavaScript-HTML </h1>
const titleTextTag = document.getElementById('title-text');
let titleText1 = titleTextTag.textContent;
let titleText2 = titleTextTag.innerText;
console.log(titleText1); //outputs: Interactive JavaScript-HTML
console.log(titleText2); //outputs:Interactive JavaScript-HTML
โข ์๋ฐ์คํฌ๋ฆฝํธ์ onclick ์ด๋ฒคํธ ์์ฑ์ ์ด์ฉํด increase, decrease ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
increaseTag.onclick = function () {
jjuCount++;
countTag.textContent = jjuCount;
}
decreaseTage.onclick = function () {
jjuCount--;
countTag.textContent = jjuCount;
}
๋๋ฒ์งธ. ์์ ๋ฒํผ์ ๋๋ฅด๋ฉด id=count 0์ ์์์ด ํด๋นํ๋ ์์ ๋ฒํผ๊ณผ ๋์ผํ ์์์ผ๋ก ๋ฐ๋๋ค.
๋ชจ๋ ์์ ๋ฒํผ์ <button class=colorBtn>
๋ผ๋ ๊ณตํต์ class ์ด๋ฆ์ ๊ฐ์ง๊ณ ์๋ค. HTML์์ id๊ฐ์ ๊ณ ์ ํ ๋จ ํ๋์ ์๋ณ ๊ฐ์ด์ง๋ง class๋ ์ด๋์๋ ๋๊ฐ์ ๋ค์ด๋ฐ ์์ฑ๊ฐ์ ๊ฐ์ ์ ์๋ค.
โ getElementsByClassName๋ฉ์๋๋ฅผ ํ์ฉํด ๋ชจ๋ colorBtn ์๋ฆฌ๋จผํธ์ ์ ๊ทผํด ์๋ฐ์คํฌ๋ฆฝํธ ๋ณ์์ ๋ด์๋ณด๊ธฐ.
const colorBtnTag = document.getElementsByClassName('colorBtn');
console.log(colorBtnTag); //outputs: [object HTMLButtonElement]??
์๋จ์ getElementById๋ฉ์๋๋ฅผ ํ์ฉํ์ ๊ฒฝ์ฐ console.log(countTag); //outputs: <div id="myNumber" class="text-xl font-bold mb-8">0</div>
์ ํํ๊ฒ ํด๋น๊ฐ์ html divํ๊ทธ ๊ฐ์ ๊ฐ์ ธ์จ๋ค. getElementsByClassName๋ฉ์๋ ์ญ์ ๊ฐ์ ์ญํ ๋ก ํด๋นํ๋ divํ๊ทธ ๊ฐ์ ๊ฐ์ ธ์ฌ๊ฑฐ ๊ฐ์ง๋ง ๋ฐฐ์ด ํํ์ [object]
๊ฐ์ ๊ฐ์ ธ์จ๋ค.
์์์ ๋งํ ๊ฒ ์ฒ๋ผ class๋ ๊ฐ์ ์ด๋ฆ์ผ๋ก ์ด๋์๋ ํ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด์ ํํ๋ก ๊ฐ์ ๊ฐ์ ธ์จ๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ getElementById๋ฉ์๋์ ๊ฐ์ ๊ฐ์ ์ป๊ธฐ ์ํด์๋ console.log(colorBtnTag[0]);
์ฒ๋ผ ๋ฐฐ์ด์ ๋๊ฐ๋ก indexํ๊ธฐ๋ฒ์ผ๋ก ๊ฐ์ ๊ฐ์ ธ์์ผ ํ๋ค.
์ฌ๊ธฐ์ ์ค์ํ ์ ์ ๋ฐฐ์ด์ ํํ๋ฅผ ๊ฐ์ง๊ณ ์์ง๋ง ์ค์ ๋ฐฐ์ด์ ์๋๋ผ๋ ๊ฒ์ด๋ค. ๋ฐฐ์ด์ ๋ค์ํ ๊ธฐ๋ณธ ๋ฉ์๋(splice,pop๋ฑ)๋ฅผ ์ฌ์ฉํ ์๊ฐ ์๋ค. ์ด๋ ๊ฒ ๋ฐฐ์ด์ ์ ์ฌ ๋ฐฐ์ด(Array-like-Object)์ด๋ผ๊ณ ๋ถ๋ฆฐ๋ค.
๐ฎ์ ์ฌ ๋ฐฐ์ด์ด๋ ๋ฌด์์ธ๊ฐ?
- ๋ฐฐ์ด๊ณผ ๊ฐ์ ์ซ์๋ก ๋ index๊ฐ์ ์ฌ์ฉํ ์ ์๋ค. ์ฐธ๊ณ ๋ก HTML์ index ์์๋ HTML ์ฝ๋์ ์์์ ์๋ ์์๋ก index๊ฐ์ด ๋งค๊ฒจ์ง๋ค.
- length๊ฐ์ด ์กด์ฌํ๋ค.
- ๋๊ฐ๋ก ํ๊ธฐ๋ฒ์ผ๋ก ํด๋นํ๋ ๊ฐ์ ์ ๊ทผํ ์ ์๋ค.colorBtnTag[0]
- ๋ฐฐ์ด๊ณผ ๊ฐ์ด for..of๋ฌธ์ด ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
- ๋ฐฐ์ด์ ๋ค์ํ ๊ธฐ๋ณธ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- Array.isArray ๊ฐ์ false์ด๋ค.
for๋ฌธ์ ์ฌ์ฉํด ๋ชจ๋ colorBtn class ์๋ฆฌ๋จผํธ์ ์ ๊ทผํด๋ณด์.
const colorBtnsGroup = document.getElementsByClassName('colorBtn');
for (let i = 0; i < colorBtnsGroup.length; i++) {
console.log(colorBtnsGroup[i]); //ouputs: <button class...> x 4
}
โก ๊ฐ ํด๋นํ๋ ์์์ onclick ์ด๋ฒคํธ ์์ฑ์ ์ด์ฉํด counterTag
์ซ์ ์์์ด ๋ฐ๋๊ฒ ๋ง๋ค์ด๋ณด๊ธฐ
const colorBtnsGroup = document.getElementsByClassName('colorBtn');
for (let i = 0; i < colorBtnsGroup.length; i++) {
colorBtnsGroup[i].onclick = function () {
counterTag.style.color = colorBtnsGroup[i].dataset.color;
}
for๋ฌธ์ ํตํด className์ผ๋ก ๊ฐ์ ธ์จ ๊ฐ <button class>
๋ค์ onclick์ด๋ฒคํธ๋ฅผ ๊ฑธ์ด์ฃผ๊ณ ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ์์ dataset ๊ฐ์ฒด๋ฅผ ํตํด ๊ฐ์ ธ์จ <button data-color="xxxxxx">
์ color๊ฐ์ counterTage.style.color ์ ํ ๋นํด์ค๋ค.
โ
dataset ๊ฐ์ฒด ํ์ฉ
HTML์์๋ ์ด๋ ์๋ฆฌ๋ฉํธ์์๋ data-
๋ก ์์ํ๋ ์์ฑ์ ์ด๋ค ๊ฐ์ผ๋ก๋ ์ถ๊ฐํด ์ค ์ ์๋ค. (ํ๋ฉด์ ๋ณด์ด์ง ์๋๋ค.)
<div
id="overwatch"
data-char="24"
data-make="billizard">
...
</div>
์ด๋ฐ์์ผ๋ก ์ถ๊ฐ๋ ๊ฐ๋ค์ JS์์๋
dataset
๊ฐ์ฒด๋ฅผ ํ์ฉํด ๊ฐ์ ์ ๊ทผํ ์ ์๋ค.
let datas = document.getElemetById('overwatch');
datas.dataset.char //"24"
datas.dataset.make //"billzard"
๋ฌธ์ ์ ์ ์ธํฐ๋ท์ต์คํ๋ก๋ฌ 11+ ์ดํ ๋ฒ์ ์์๋ dataset์ ์ง์ํ์ง ์๊ณ ,์๋์ ์ธ ๋ฉด์์๋ ์ถ์ฒํ์ง ์๋๋ค๊ณ ํ๋ค.
for๋ฌธ ๋์ for..of๋ฌธ์ ์ฌ์ฉํด๋ ๋๋ค.
const colorBtnsGroup = document.getElementsByClassName('colorBtn');
for (let btn of colorBtnsGroup) {
btn.onclick = function () {
countTag.style.color = btn.dataset.color
}
}
๐โgetElementById์ getElementsByClassName ์ฌ์ฉ์ ์ฐพ์ ์ ์๋ ๊ฐ์ ์ถ๋ ฅํ๋ฉด ์ด๋ค ๊ฐ์ด ๋์ฌ๊น?
const searchId = document.getElementById('noSearch');
const searchClass = document.getElementsByClassName('noSearch');
console.log(searchId); //outputs: null
console.log(searchClass); //outputs: HTMLCollection[]
๐ฒ์ต์ข index.js ์ฝ๋
const countTag = document.getElementById("count");
const increaseTag = document.getElementById("plusBtn");
const decreaseTag = document.getElementById("minusBtn");
let jjuCount = +countTag.textContent;
increaseTag.onclick = () => {
jjuCount++;
countTag.textContent = jjuCount;
};
decreaseTag.onclick = () => {
jjuCount--;
countTag.textContent = jjuCount;
};
let colorBtnsGroup = document.getElementsByClassName("colorBtn");
for (let i = 0; i < colorBtnsGroup.length; i++) {
colorBtnsGroup[i].onclick = function () {
countTag.style.color = colorBtnsGroup[i].dataset.color;
};
}
'๐ WELL-DONE Project > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
this ๊ฐ๋ ์ดํดํ๊ธฐ (0) | 2021.02.17 |
---|---|
๋ธ๋ผ์ฐ์ ์์์ ์๋ฐ์คํฌ๋ฆฝํธ ๋์ ์์๋ณด๊ธฐ.2 (0) | 2020.12.18 |
๋ธ๋ผ์ฐ์ ์์์ ์๋ฐ์คํฌ๋ฆฝํธ ๋์ ์์๋ณด๊ธฐ.1 (0) | 2020.12.17 |
์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ์์ด(String) ์ตํ๊ธฐ - ๋ฐฐ์ด(Array)๊ณผ ๊ณตํต์ &์ฐจ์ด์ (0) | 2020.12.08 |