๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ– WELL-DONE Project/Javascript

HTML์—์„œ์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ.

์ปจํ…์ธ ์˜ ๋‚ด์šฉ์€ '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>

ex_img

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;
  };
}