๐Ÿ“ŒDevelopment/JavaScript

[JS] DOM & EVENT #5 ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ(Event Handler)

geumsong 2023. 3. 27. 01:19

1. Event Handler ํ• ๋‹นํ•˜๊ธฐ

<!DOCTYPE html>
<html>
  <head>
    <title>Welcome</title>
  </head>
  <body>
    <button onclick = "alert('ํด๋ฆญํ–ˆ์Šต๋‹ˆ๋‹ค')"> ํด๋ฆญ </button>
  </body>

 

 

DOM Events

 

HTML DOM Event Object

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

: Event์˜ ์†์„ฑ๋“ค์ด ์žˆ๋‹ค. ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๋ฉด ์ข‹๋‹ค.

 

 

 

<!DOCTYPE html>
<html>
  <head>
    <title>Welcome</title>
  </head>
  <body>
    <button onclick="alert('ํด๋ฆญํ–ˆ์Šต๋‹ˆ๋‹ค')">ํด๋ฆญ</button>
    <button onclick="sayHello()">ํด๋ฆญ2</button>
    <button id="btn">ํด๋ฆญ3</button>
    <button id="btn2">ํด๋ฆญ4</button>

    <script>
      function sayHello() {
        alert("Hello"); //๊ด„ํ˜ธ๊ฐ€ ์—†์–ด์•ผํ•œ๋‹ค. ๊ด„ํ˜ธ๊ฐ€ ์žˆ์œผ๋ฉด ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด ํ• ๋‹น๋˜๊ธฐ ๋•Œ๋ฌธ.
      }

      const el = document.getElementById("btn");
      el.onclick = sayHello

      const el2 = document.getElementById("btn2");
      el2.addEventListener("click", sayHello); //addEventListener: ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ•ธ๋“ค๋Ÿฌ(์ „๋‹ฌ๋œ ํ•จ์ˆ˜)๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

    </script>
  </body>
</html>

์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

ํด๋ฆญ, ํด๋ฆญ2, ํด๋ฆญ3, ํด๋ฆญ4๋ฅผ ๋ณด๋ฉด ๊ฐ์ž ๋‹ค๋ฅธ ๋ฐฉ์‹์˜ button event ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

   addEventListener

  • :ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ•ธ๋“ค๋Ÿฌ(์ „๋‹ฌ๋œ ํ•จ์ˆ˜)๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  • Handler๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค

 

 el2.addEventListener("click", sayHello);

sayHello ์ž๋ฆฌ์—” ์ง์ ‘ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

el2.addEventListener("click", () => {
alert("hi");
});

 

๊ฒฐ๊ณผ

 

 

+  removeEventListener

: ์ด๋ฏธ ํ• ๋‹น๋œ ์ด๋ฒคํŠธ๋ฅผ ์ง€์šธ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

 


 

   focus์™€ blur

  • focus: ์ž…๋ ฅ์ฐฝ์— ๋งˆ์šฐ์Šค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ๋ฐœ์ƒ
  • blur: focus ์žƒ์„ ๋•Œ

โœ๏ธ์ž…๋ ฅ

input.addEventListener("focus", () => {
  input.style.backgroundColor = "rgba(255,-0,0,0.2)";
  });
  
  input.addEventListener("blur", () => {
    input.style.backgroundColor = null; // null: ์ดˆ๊ธฐํ™”์‹œํ‚จ๋‹ค
    });

๐Ÿ“๊ฒฐ๊ณผ

  • ์ž…๋ ฅ์ฐฝ ํด๋ฆญ์‹œ ์„ค์ •ํ•œ rgb๊ฐ’์˜ ์ƒ‰์ด ๋ณด์—ฌ์ง€๊ณ , ํฌ์ฟ ์Šค๋ฅผ ๋–ผ๋ฉด ์ดˆ๊ธฐํ™”์‹œํ‚จ๋‹ค.

 


 

   mousemove

  • ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์˜€์„๋•Œ๋งˆ๋‹ค ์ด๋ฒคํŠธ ๋ฐœ์ƒ

โœ๏ธ์ž…๋ ฅ

<!DOCTYPE html>
<html>
  <head>
    <title>Welcome</title>
  </head>
  <body>
    <div
      id="box"
      style="width: 100px; height: 100px; border: 2px solod red"
    ></div>  
    <script>
     const box = document.getElementById("box");
     box.addEventListener("mousemove", event => {
     console.log(event);
     });
    </script>
  </body>
</html>

๐Ÿ“๊ฒฐ๊ณผ

: ์ด ๋„ค๋ชจ๋ฐ•์Šค ์•ˆ์—์„œ ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์ผ ๋•Œ๋งˆ๋‹ค console์—์„œ ๋งˆ์šฐ์Šค์˜ ์œ„์น˜๊ฐ’์ด ๊ณ„์† ๋œจ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

: ์ž˜ ์•ˆ๋ณด์ด๊ฒ ์ง€๋งŒ..

clientX: 54

clientY: 111 ์ด๋ผ๊ณ  ์ ํ˜€์žˆ๋‹ค.

์ด๊ฒƒ์„ ํ†ตํ•ด ํ˜„์žฌ ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ์˜ ์œ„์น˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

โœจ์‹ฌํ™”) ์ปค์„œ๋ฅผ ๋”ฐ๋ผ๋‹ค๋‹ˆ๋Š” ๋™๊ทธ๋ผ๋ฏธ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

โœ๏ธ์ž…๋ ฅ

  • ์ฒœ์ฒœํžˆ ๋”ฐ๋ผํ•˜๋ฉด์„œ ์ดํ•ดํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.
<!DOCTYPE html>
<html>
  <head>
    <title>Welcome</title>
  </head>
  <body>
    <!DOCTYPE html>
    <html>
      <head>
        <title>Welcome</title>
      </head>
      <body>
        <div
          id="box"
          style="
            position: relative; 
            width: 100px;
            height: 100px;
            border: 2px solid red
          "
        >
        <div
          id="circle"
          style="
            position: absolute;
            width: 10px;
            height: 10px;
            background-color: #000;
            border-radius: 50%;
          "
        ></div>
        <script>
         const box = document.getElementById("box");
         box.addEventListener("mousemove", event => {
           circle.style.top = `${event.clientY}px`;
           circle.style.left = `${event.clientX}px`;
         });
        </script>
      </body>
    </html>
    </script>
  </body>
</html>

๐Ÿ“๊ฒฐ๊ณผ

  • ์ปค์„œ๊ฐ€ ๋„ค๋ชจ ๋ฐ•์Šค ์•ˆ์— ์žˆ์„๋•Œ ๋งˆ์šฐ์Šค์˜ค๋ฒ„์‹œ, ๊ฒ€์ •์ƒ‰ ์›์ด ์ปค์„œ๋ฅผ ๋”ฐ๋ผ๋‹ค๋‹Œ๋‹ค. 
  • ๋งˆ์šฐ์Šค๊ฐ€ ์›€์ง์ผ๋•Œ๋งˆ๋‹ค ์›์˜ top๊ณผ left์˜ ๊ฐ’์ด ๋ฐ”๋€๋‹ค.

 


   resize

  • ์ฐฝ ํฌ๊ธฐ๊ฐ€ ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค, ๋„“์ด์™€ ๋†’์ด๊ฐ’์ด ๋‹ฌ๋ผ์ง„๋‹ค.

โœ๏ธ์ž…๋ ฅ

<!DOCTYPE html>
<html>
  <head>
    <title>Welcome</title>
  </head>
  <body>
    <!DOCTYPE html>
    <html>
      <head>
        <title>Welcome</title>
      </head>
      <body>
        <script>
         window.addEventListener("resize", () => {
            document.body.innerText = `ํ˜„์žฌ ์ฐฝ ํฌ๊ธฐ๋Š” ${window.
            innerWidth} x ${window.innerHeight}`;
         });
        </script>
      </body>
    </html>
    </script>
  </body>
</html>

๐Ÿ“๊ฒฐ๊ณผ

 

 

์ด๋ฒคํŠธ type์€ ์ƒ๋‹นํžˆ ๋งŽ๋‹ค. 

๊ด€๋ จ๋œ type๋“ค์„ ์‚ฌ์šฉํ•ด๋ณด๋ฉด์„œ ์–ด๋–ค ๊ธฐ๋Šฅ์ธ์ง€ ์—ฐ์Šต์„ ํ•ด๋ณด๊ธฐ๋ฅผ ๊ถŒํ•œ๋‹ค.

 

 

 

์ž์ฃผ ์“ฐ๋Š” EVENT type
click ํด๋ฆญ ์‹œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ.
dblclick ๋”๋ธ”ํด๋ฆญ ์‹œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ.
keydown ํ‚ค๋ณด๋“œ๋ฅผ ๋—์„ ๋•Œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ
keyup ํ‚ค๋ณด๋“œ๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ
focus // inputํƒœ๊ทธ์†์„ฑ ์ž…๋ ฅ์ฐฝ์— ๋งˆ์šฐ์Šค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ๋ฐœ์ƒ
focusin // inputํƒœ๊ทธ์†์„ฑ  
blur // inputํƒœ๊ทธ์†์„ฑ focus๋ฅผ ์žƒ์„ ๋•Œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ
focusout/ / inputํƒœ๊ทธ์†์„ฑ  
mousemove ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์ผ๋•Œ๋งˆ๋‹ค ์ด๋ฒคํŠธ ๋ฐœ์ƒ
resize ์ฐฝ ํฌ๊ธฐ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค, ๋„ˆ๋น„์™€ ๋†’์ด๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค