TIL

๐Ÿ“— ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive | ์ด๋ฒคํŠธ [์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ]

grasinnong 2022. 5. 26. 16:12

 

 

 

์šฉ์–ด ์ •๋ฆฌ

- ์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง(event bubbling) ์ด๋ฒคํŠธ ํƒ€๊นƒ ์š”์†Œ์— ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, ์ตœ์ƒ๋‹จ์˜ ๋ถ€๋ชจ ์š”์†Œ๊นŒ์ง€ ๊ฐ๊ฐ ํ• ๋‹น๋œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ๋˜๋Š” ๊ฒƒ. 

 

- ์ด๋ฒคํŠธ ์บก์ฒ˜๋ง(event capturing) 

  3๋‹จ๊ณ„์˜ ์ด๋ฒคํŠธ ํ๋ฆ„์—์„œ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„์ด๋‹ค. 

  1. ์บก์ฒ˜๋ง ๋‹จ๊ณ„ : ์ด๋ฒคํŠธ๊ฐ€ ํ•˜์œ„ ์š”์†Œ๋กœ ์ „ํŒŒ๋˜๋Š” ๋‹จ๊ณ„ 

  2. ํƒ€๊นƒ ๋‹จ๊ณ„

  3. ๋ฒ„๋ธ”๋ง ๋‹จ๊ณ„

 

  ๊ฐ€์žฅ ์ตœ์ƒ์œ„ ์กฐ์ƒ์—์„œ ์•„๋ž˜(ํƒ€๊นƒ ์š”์†Œ)๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ๋‹จ๊ณ„์ด๋‹ค. 

 

 

 

 

 


 

์ด๋ฒคํŠธ(Event)

 

๋ธŒ๋ผ์šฐ์ €์—์„œ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ, ๋’ค๋กœ ๊ฐ€๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ๋ธŒ๋ผ์šฐ์ €๋Š” ๋ฒ„ํŠผ์˜ ์—ญํ• ์— ๋งž๊ฒŒ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋–ค ์‚ฌ๊ฑด(๋ฒ„ํŠผ ํด๋ฆญ, ๋”๋ธ”ํด๋ฆญ, enter ๋“ฑ)์„ ๋ฐœ์ƒ์‹œ์ผฐ์„ ๋•Œ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ด๋ฅผ ๊ฐ์ง€ํ•˜์—ฌ ์ด๋ฒคํŠธ(๋กœ๊ทธ์ธ์„ ํ•œ๋‹ค, ๋‹ค์Œ ํŽ˜์ด์ง€๋กœ ๋„˜๊ธด๋‹ค ๋“ฑ)๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. 

 

์œ„์˜ ์ฝ”๋“œ๋Š” ๋ฒ„ํŠผ์— ํด๋ฆญ(onclick)์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๋•Œ console.log("button clicked!") ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ์ด๋ฒคํŠธ์— ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ(ํ•จ์ˆ˜)๋ฅผ ํ• ๋‹นํ•˜๋ฉด, ํ•ด๋‹น ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

 

 

๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ ๋ชฉ๋ก์€ ์•„๋ž˜์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฒคํŠธ ์ฐธ์กฐ | MDN

DOM ์ด๋ฒคํŠธ๋Š” ๋ฐœ์ƒํ•œ ํฅ๋ฏธ๋กœ์šด ๊ฒƒ์„ ์ฝ”๋“œ์— ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์ด๋ฒคํŠธ๋Š” Event ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœํ•œ ๊ฐ์ฒด์— ์˜ํ•ด ํ‘œํ˜„๋˜๋ฉฐ ๋ฐœ์ƒํ•œ ๊ฒƒ์— ๋Œ€ํ•œ ๋ถ€๊ฐ€์ ์ธ ์ •๋ณด๋ฅผ ์–ป๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ถ”๊ฐ€

developer.mozilla.org

 

 

 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ(Event Handler) 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹คํ–‰๋  ๋™์ž‘์ด๋‹ค.

 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

 

1. ์†์„ฑ์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ธฐ

 

HTML Event Attributes

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

 html event attributes๋ฅผ ์ด์šฉํ•œ ๋ฐฉ์‹์ด๋‹ค. ๋‹ค๋ฅธ ์†์„ฑ์ฒ˜๋Ÿผ ์ด๋ฒคํŠธ ์†์„ฑ์˜ ๊ฐ’์œผ๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. 

์ฃผ์˜์‚ฌํ•ญ : ํ•จ์ˆ˜ ํ• ๋‹น(ํ•จ์ˆ˜ ์ฐธ์กฐ)์ด ์•„๋‹Œ, ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <button onclick="alert('button clicked!')">click!</button>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <button onclick="onclickHandler();">click!</button>
    <script>
      const onclickHandler = () => {
        alert("button clicked!");
      };
    </script>
  </body>
</html>

ํ•จ์ˆ˜ ์ฐธ์กฐ๊ฐ€ ์•„๋‹Œ, ํ•จ์ˆ˜ ํ• ๋‹น์„ ํ•˜๋ฉด ํ•จ์ˆ˜ ๊ฒฐ๊ณผ ๊ฐ’์ด ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋กœ ๋“ฑ๋ก๋œ๋‹ค. 

 

 

- ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€์˜ this : window ๊ฐ์ฒด 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <button onclick="onclickHandler(this);">click!</button>
    <script>
      const onclickHandler = (val) => {
        console.log(val);//<button onclick="onclickHandler(this);">click!</button>
      };
    </script>
  </body>
</html>

 

 

2. ํ”„๋กœํผํ‹ฐ๋กœ ๋“ฑ๋กํ•˜๊ธฐ 

 

์ด๋ฒคํŠธ๋ฅผ ๊ฑธ์–ด์ค„ ์š”์†Œ์— ์ด๋ฒคํŠธ ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <button>click!</button>
    <script>
      $target = document.querySelector("button");

      $target.onclick = () => {
        alert("button clicked!");
      };
    </script>
  </body>
</html>

 

- ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€์˜ this : ์ด๋ฒคํŠธ๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ DOM ์š”์†Œ 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <button class="btn01">click!</button>
    <script>
      $target = document.querySelector(".btn01");

      $target.onclick = function(e){
        console.log(this); //<button class="btn01">click!</button>
        console.log(this === e.currentTarget); //true
      };
    </script>
  </body>
</html>

 

 

 

 

3. addEventListener ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ 

 

[๊ตฌ๋ฌธ]

addEventListener(eventType, eventHandler, options, useCapture);

parameter

 

- type : ์ด๋ฒคํŠธ ์œ ํ˜•

eventHandler : ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜ ์ด๋ฆ„ 

- options

  - capture : ์ด๋ฒคํŠธ ์บก์ฒ˜๋ง ๋‹จ๊ณ„์—์„œ ์ด๋ฒคํŠธ๋ฅผ ์‹คํ–‰ํ• ์ง€ ์„ค์ • | true or false(default : ๋ฒ„๋ธ”๋ง ๋‹จ๊ณ„์—์„œ ์ด๋ฒคํŠธ ๋™์ž‘)

  - once : ๋”ฑ ํ•œ ๋ฒˆ๋งŒ ์ด๋ฒคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์ด๋ฒคํŠธ๋ฅผ ์ œ๊ฑฐํ•  ์ง€ ์„ค์ • | true or false(default)

  - passive : preventDefault()๋ฅผ ํ˜ธ์ถœํ• ์ง€ ์„ค์ • | true or false(default)

  - signal : 

- useCapture : ์บก์ฒ˜๋ง, ๋ฒ„๋ธ”๋ง์ˆœ์„œ ์กฐ์ ˆ | true or false(default)

 

 

return 

๋ฐ˜ํ™˜ ๊ฐ’ ์—†์Œ.

 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <button>click!</button>
    <script>
      $target = document.querySelector("button");

      $target.addEventListener("click", () => {
        alert("clicked!");
      });
    </script>
  </body>
</html>

 

- ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€์˜ this : ์ด๋ฒคํŠธ๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ DOM ์š”์†Œ 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <button>click!</button>
    <script>
      $target = document.querySelector("button");

      $target.addEventListener("click", function(e){
        console.log(this);//<button>click!</button>
        console.log(this === e.currentTarget); //true
      });
    </script>
  </body>
</html>

 

 

์ด๋ฒคํŠธ ๊ฐ์ฒด

 

์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๐Ÿ‘‰ ์ด๋ฒคํŠธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ๐Ÿ‘‰ ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ž๋™์œผ๋กœ ๋‹ด๊ธด๋‹ค. 

 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <button>click!</button>
    <script>
      $target = document.querySelector("button");

      $target.onclick = function(){
        alert("clicked 1!");
      };

      $target.addEventListener("click", (eventObj) => {
        console.log(eventObj);
      });
    </script>
  </body>
</html>

 

์ด๋ฒคํŠธ ๊ฐ์ฒด์—๋Š” ์ด๋ฒคํŠธ์™€ ๊ด€๋ จ๋œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค.

 

์˜ˆ์ œ 1) 

<html>
    <head>
			<style>
				#wrap{
					position : fixed;
					bottom : 0;
				}
				.infobox{
					border:10px solid skyblue;
					display : inline-block;
					background-color: yellow;
				}
			</style>
    </head>
    <body>
			<h1>ํด๋ฆญ์‹œ ๋งˆ์šฐ์Šค์ขŒํ‘œ / ๋ฆฌ์‚ฌ์ด์ง• ์‹œ ์ฐฝ์˜ ํฌ๊ธฐ / ์Šคํฌ๋กค ์‹œ ์Šคํฌ๋กค๊ธธ์ด</h1>
			<div id="wrap">
				<h3 id="info1" class="infobox"></h3>
				<h3 id="info2" class="infobox"></h3>
				<h3 id="info3" class="infobox"></h3>
			</div>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>
			<p>some scroll here.....</p>

      <script>
        $html = document.querySelector("html");
        $html.addEventListener("click", function(e){
          //ํด๋ฆญ ์‹œ ๋งˆ์šฐ์Šค x์™€ y์ขŒํ‘œ 
          let x = e.pageX;
          let y = e.pageY;
          document.querySelector("#info1").textContent = `x์ขŒํ‘œ : ${x} , y์ขŒํ‘œ : ${y}`;
        });

        window.addEventListener("resize", function(e){
          //์ฐฝ ๋„ˆ๋น„ ๋ณ€๊ฒฝ ์‹œ ์ฐฝ์˜ width, height
          let w = e.target.innerWidth;
          let h = e.target.innerHeight;

          document.querySelector("#info2").textContent = `์ฐฝ์˜ ๋„ˆ๋น„ : ${w}, ์ฐฝ์˜ ๋†’์ด : ${h}`;
        });

        window.addEventListener("scroll",function(e){
          //์Šคํฌ๋กค ์‹œ ์Šคํฌ๋กค ๋œ ๊ธธ์ด 
          let length = this.scrollY;
          document.querySelector("#info3").textContent = `์Šคํฌ๋กค๊ธธ์ด : ${length}`;
        });
      </script>
    </body>
</html>

 

 

 

 

 

 

 

 

 

 

๋ฒ„๋ธ”๋ง๊ณผ ์บก์ฒ˜๋ง

 

ko.javascript.info