GiantStepDEV
article thumbnail

PointerEvent ์†์„ฑ

KeyboardEvent ๊ฐ์ฒด ํ™•์ธ

keyCode => key
const btnEl = document.querySelector("button");
btnEl.addEventListener("click", function(e) {
  console.log("๋ฒ„ํŠผ์ด ๋ˆŒ๋ ค์กŒ์Šต๋‹ˆ๋‹ค." + e);
  console.log("x์ขŒํ‘œ : " + e.clientX);
  console.log("y์ขŒํ‘œ : " + e.clientY);
  console.log("๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ x ์ขŒํ‘œ : " + e.pageX);
  console.log("๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ y ์ขŒํ‘œ : " + e.pageY);
  console.log("๋ชจ๋‹ˆํ„ฐ์˜ ์™ผ์ชฝ ๋ชจ์„œ๋ฆฌ ๊ธฐ์ค€์œผ๋กœ ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ x ์ขŒํ‘œ : " + e.screenX);
  console.log("๋ชจ๋‹ˆํ„ฐ์˜ ์™ผ์ชฝ ๋ชจ์„œ๋ฆฌ ๊ธฐ์ค€์œผ๋กœ ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ y์ถ• ์ขŒํ‘œ : " + e.screenY);
});

const inputEl = document.querySelector("input");
inputEl.addEventListener("keydown", function(event) {
  console.log(`key : ${event.key}`);
  console.log(`ctrlKey : ${event.ctrlKey}`);
  console.log(`altKey : ${event.altKey}`);
  console.log(`shiftKey : ${event.shiftKey}`);
});

์ด๋ฒคํŠธ ์ทจ์†Œํ•˜๊ธฐ

  • HTML ํƒœ๊ทธ ์ค‘ ์ผ๋ถ€๋Š” ๊ธฐ๋ณธ์œผ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
    ๋Œ€ํ‘œ์ ์œผ๋กœ a ํƒœ๊ทธ์™€ form ํƒœ๊ทธ๊ฐ€ ํ•ด๋‹น ๋ฉ๋‹ˆ๋‹ค.
  • target ์†์„ฑ์€ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์š”์†Œ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. (React์—์„œ ํ™•์ธ ์˜ˆ์ •)
  • preventDefault() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒœ๊ทธ์— ๊ธฐ๋ณธ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ์ด๋ฒคํŠธ๋ฅผ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

form ํƒœ๊ทธ์— addEventListner() ๊ฑธ ๋•Œ ์ •๋ณด๋ฅผ ์„œ๋ฒ„์— ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ๋•Œ ๊ธฐ๋ณธ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ์ด๋ฒคํŠธ๋ฅผ ์ทจ์†Œํ•ด์ค˜์•ผ ํ•จ.

const aEls = document.querySelectorAll("a");
for(let e of aEls) {
  e.addEventListener("click", function(event) {
    // ๊ธฐ๋ณธ ์ด๋ฒคํŠธ ์ทจ์†Œ
    event.preventDefault();
  });
}

const formEl = document.getElementById("myForm");
formEl.addEventListener("submit", function(event) {
  // ๊ธฐ๋ณธ ์ด๋ฒคํŠธ ์ทจ์†Œ
  event.preventDefault(); // ํผ ์ œ์ถœ ๊ธฐ๋ณธ ๋™์ž‘ ๋ง‰๊ธฐ
});

this ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

  • ์ด๋ฒคํŠธ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ฐ”๋กœ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    (์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์€ html ์š”์†Œ)
const pEls = document.querySelectorAll("#p1");
const pEls2 = document.querySelectorAll("#p2");
// for(let el of pEls) {}; ๊ณผ pEls.forEach((el) => {});์€ ๊ฐ™๋‹ค.
pEls.forEach((el) => {
  el.addEventListener("click", function() {
    if(this.style.color === "red") { // ์—ฌ๊ธฐ์„œ์˜ this๋Š” ํ˜„์žฌ ์ด๋ฒคํŠธ๊ฐ€ ๋ˆŒ๋ ค์ง„ ์š”์†Œ
      this.style.color = "black";
    } else this.style.color = "red";
  });
});

pEls2.forEach((el) => {
  el.addEventListener("click", () => {
    if(el.style.color === "red") {
      el.style.color = "black";
    } else el.style.color = "red";
  });
});

this ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ์‹œ ์ฃผ์˜ ํ•  ์ !
์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ์ž‘์„ฑํ•˜๋ฉด this์˜ ๋ฒ”์œ„๊ฐ€ ๋‹ฌ๋ผ์ ธ์„œ this๊ฐ€ ์ด๋ฒคํŠธ ๋ฐœ์ƒ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
(์›๋ž˜ addEventListener()์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜์—์„œ๋Š” this์— ํ•ด๋‹น ์ด๋ฒคํŠธ๊ฐ€ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ํ˜ธ์ถœ๋œ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋˜๋„๋ก ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ์ด๋ฏธ this์˜ ๊ฐ’์ด ์ •ํ•ด์ ธ ์žˆ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด ๋ฐ”์ธ๋”ฉ ๊ฐ’์ด ์‚ฌ๋ผ์ง€๊ณ  ์ƒ์œ„ ์Šค์ฝ”ํ”„๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์˜๋„ํ–ˆ๋˜ ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <p id="p1">text-1</p>
  <p id="p1">text-2</p>
  <p id="p1">text-3</p>
  <br>
  <p id="p2">test-1</p>
  <p id="p2">test-2</p>
  <p id="p2">test-3</p>

  <script src="./230403_์ด๋ฒคํŠธthis.js"></script>
</body>
</html>


์‹ค์Šต ์˜ˆ์ œ - ์˜ํ™”๊ด€ ์˜ˆ๋งค

  • ๋ฉ”๋‰ด๋Š” [1] ์˜ˆ๋งคํ•˜๊ธฐ [2] ์ •์‚ฐํ•˜๊ธฐ
  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ขŒ์„๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ์˜ˆ๋งคํ•˜๋Š” ์‹œ์Šคํ…œ์ด๋‹ค. (์ขŒ์„์€ ์ด 10๊ฐœ)
  • ์ด๋ฏธ ์˜ˆ๋งค๊ฐ€ ์™„๋ฃŒ๋œ ์ขŒ์„์€ ์žฌ๊ตฌ๋งค ๋ถˆ๊ฐ€
  • ํ•œ ์ขŒ์„๋‹น ๊ฐ€๊ฒฉ์€ 12000์›
  • [์ •์‚ฐํ•˜๊ธฐ] ํด๋ฆญ์‹œ ํ•ด๋‹น ์˜ํ™”๊ด€์˜ ์ด ๋งค์ถœ์•ก ์ถœ๋ ฅ
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./230403_3_์˜ํ™”ํ‘œ์˜ˆ๋งค.css">
  <title>Document</title>
</head>
<body>
  <h1>๐ŸŒฎ ์˜ˆ๋งค ์‹œ์Šคํ…œ ๐Ÿฟ</h1>
  <div class="button-container">
    <button id="reserve">์˜ˆ๋งคํ•˜๊ธฐ</button>
    <button id="quit">์ •์‚ฐํ•˜๊ธฐ</button>
  </div>
  <div id="seat">
    <div class="seatInput">
      <label for="seatNum">์ขŒ์„ ๋ฒˆํ˜ธ</label>
      <input type="text" id="seatNum">
      <button type="submit" id="reserveBtn">์˜ˆ๋งค</button>
    </div>
    <div class="seat-container">
      <div class="seat-item 1"></div>
      <div class="seat-item 2"></div>
      <div class="seat-item 3"></div>
      <div class="seat-item 4"></div>
      <div class="seat-item 5"></div>
      <div class="seat-item 6"></div>
      <div class="seat-item 7"></div>
      <div class="seat-item 8"></div>
      <div class="seat-item 9"></div>
      <div class="seat-item 10"></div>
    </div>
  </div>
  <h1 id="result"></h1>
  <script src="./230403_3_์˜ํ™”ํ‘œ์˜ˆ๋งค.js"></script>
</body>
</html>
* {
  box-sizing: border-box;
}
body {
  width: 100%;
}
#seat {
  display: none;
}
.seatInput {
  display: flex;
  margin: 20px 0;
  justify-content: center;
  align-items: center;
  gap: 10px;
}
.button-container {
  display: flex;
  gap: 10px;
  justify-content: center;
  align-items: center;
}
.button-container button {
  width: 80px;
  height: 40px;
  background-color: #e2e7f7;
  border: none;
  box-shadow: 1px 1px 1px #696969;
  cursor: pointer;
}
#reserveBtn {
  background-color: #e2e7f7;
  cursor: pointer;
  border: none;
  box-shadow: 1px 1px 1px #696969;
}
.seat-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 20px 0;
  gap: 10px;
}
.seat-item {
  width: 40px;
  height: 40px;
  border: 1px solid #696969;
}
#result, h1 {
  text-align: center;
}
const reserveEl = document.querySelector("#reserve");
reserveEl.addEventListener("click", function() {
  const seatEl = document.querySelector("#seat");
  if(seatEl.style.display == 'none') {
    seatEl.style.display = 'block';
  } else seatEl.style.display = 'none';
});

const seat = document.querySelectorAll(".seat-item");

const reserveBtn = document.querySelector("#reserveBtn");
reserveBtn.addEventListener("click", function() {
  const seatNum = document.getElementById("seatNum").value;
  for(let e of seat) {
    let tmp = e.className.substring(10);
    if(tmp == seatNum) {
      if(e.style.backgroundColor == "royalblue") {
        alert("์ด๋ฏธ ์˜ˆ๋งค๊ฐ€ ์™„๋ฃŒ๋œ ์ขŒ์„ ์ž…๋‹ˆ๋‹ค.");
      } else {
        e.style.backgroundColor = "royalblue";
        alert("์˜ˆ๋งค๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
      }
      const inputBox = document.querySelector("#seatNum");
      inputBox.value = "";
      document.getElementById("seatNum").focus();
    }
  }
});

const quitEl = document.querySelector("#quit");
quitEl.addEventListener("click", function() {
  let cnt = 0;
seat.forEach((e) => {
  if(e.style.backgroundColor == "royalblue") {
    cnt++;
  }
});
document.getElementById("result").innerHTML = "์ด ๋งค์ถœ : " + cnt * 12000 + "์›";
});

profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!