JavaScript(6)

zooyeongยท2023๋…„ 3์›” 20์ผ

7์ฃผ์ฐจ

๋ชฉ๋ก ๋ณด๊ธฐ
7/7
post-thumbnail

๐Ÿ’ก์ด๋ฒคํŠธ ๋‹ค๋ฃจ๊ธฐ

๐Ÿ“–์ด๋ฒคํŠธ ์ข…๋ฅ˜

  1. ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ
  • onclick : ๋งˆ์šฐ์Šค๋กœ ํด๋ฆญํ•˜๋ฉด ๋ฐœ์ƒ
  • ondblclick : ๋งˆ์šฐ์Šค๋กœ ๋”๋ธ” ํด๋ฆญํ•˜๋ฉด ๋ฐœ์ƒ
  • onmouseover : ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ๋ฅผ ์˜ฌ๋ฆฌ๋ฉด ๋ฐœ์ƒ
  • onmouseout : ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ๊ฐ€ ๋น ์ ธ๋‚˜๊ฐ€๋ฉด ๋ฐœ์ƒ
  • onmousemove : ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ๊ฐ€ ์›€์ง์ด๋ฉด ๋ฐœ์ƒ
  • onwheel : ๋งˆ์šฐ์Šค ํœ (wheel)์„ ์›€์ง์ด๋ฉด ๋ฐœ์ƒ
  1. ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ
  • onkeypress : ํ‚ค๋ณด๋“œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ์žˆ๋Š” ๋™์•ˆ ๋ฐœ์ƒ
  • onkeydown : ํ‚ค๋ณด๋“œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ์ˆœ๊ฐ„ ๋ฐœ์ƒ
  • onkeyup : ํ‚ค๋ณด๋“œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €๋‹ค๊ฐ€ ๋—€ ์ˆœ๊ฐ„ ๋ฐœ์ƒ
  1. ํฌ์ปค์Šค ์ด๋ฒคํŠธ
  • onfocus : ์š”์†Œ์— ํฌ์ปค์Šค๊ฐ€ ๋˜๋ฉด ๋ฐœ์ƒ
  • onblur : ์š”์†Œ๊ฐ€ ํฌ์ปค์Šค๋ฅผ ์žƒ์œผ๋ฉด ๋ฐœ์ƒ
  1. ํผ ์ด๋ฒคํŠธ
  • onsubmit : ํผ์ด ์ „์†ก๋  ๋•Œ ๋ฐœ์ƒ
  1. ๋ฆฌ์†Œ์Šค ์ด๋ฒคํŠธ
  • onload : ์›น ๋ธŒ๋ผ์šฐ์ €์˜ ๋ฆฌ์†Œ์Šค ๋กœ๋“œ๊ฐ€ ๋๋‚˜๋ฉด ๋ฐœ์ƒ

๐Ÿ“–์ด๋ฒคํŠธ ๋“ฑ๋ก

์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ๋งˆ๋‹ค ์–ด๋–ค ์ž‘์—…์„ ํ• ์ง€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • ์ธ๋ผ์ธ : HTML ํƒœ๊ทธ์— ์†์„ฑ์œผ๋กœ ์ด๋ฒคํŠธ ๋“ฑ๋ก
  • ํ”„๋กœํผํ‹ฐ ๋ฆฌ์Šค๋„ˆ : ์š”์†Œ ๋…ธ๋“œ์— ์ง์ ‘ ์†์„ฑ์œผ๋กœ ์ด๋ฒคํŠธ ๋“ฑ๋ก
  • ์ด๋ฒคํŠธ ๋“ฑ๋ก ๋ฉ”์„œ๋“œ : addEventListener() ๋ฉ”์„œ๋“œ๋กœ ์ด๋ฒคํŠธ ๋“ฑ๋ก

โœ…๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ

ํด๋ฆญ

<input type="text" id="inp">
<button id="btn" onclick="func_alert()">๋ฒ„ํŠผ</button>
<script>
  function func_alert(){
    alert('๋ฒ„ํŠผ ๋ˆŒ๋ฆผ');
  }
</script>

โ†“ํด๋ฆญ

๋”๋ธ”ํด๋ฆญ

<input type="text" id="inp">
<button id="btn" ondblclick="func_alert_dbl()">๋ฒ„ํŠผ</button>
<script>
  function func_alert_dbl(){
    alert('๋ฒ„ํŠผ ๋ˆŒ๋ฆผ ๋”๋ธ”ํด๋ฆญ');
  }
</script>

โ†“๋”๋ธ”ํด๋ฆญ


โœ…ํฌ์ปค์Šค ์ด๋ฒคํŠธ

onfocus : ์ธ๋ผ์ธ๋ฐฉ์‹

<input type="text" id="inp" onfocus="inputfocus()">
<button id="btn">๋ฒ„ํŠผ</button>
<script>
  function inputfocus(){
    console.log('input ํฌ์ปค์‹ฑ๋จ')
 }
</script>

onfocus : ์ •์˜๋œ ํ•จ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

<input type="text" id="inp">
<button id="btn">๋ฒ„ํŠผ</button>
<script>
  const inpEl = document.getElementById('inp');
  inpEl.onfocus = inputfocus; //*inputfocus(); X
  function inputfocus(){
    console.log('input ํฌ์ปค์‹ฑ๋จ')
 }
</script>

โ†“onfocus

*inputfocus(); X
-> ๊ด„ํ˜ธ๋ฅผ ๋„ฃ์–ด ์จ์ฃผ๊ฒŒ ๋˜๋ฉด onfocus์ด๋ฒคํŠธ์™€ ๊ด€๊ณ„์—†์ด ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค. JS ํŒŒ์ผ์„ ์ฝ์–ด ๋‚ด๋ ค๊ฐ€๋Š” ์ˆœ๊ฐ„ inputfocus();๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜ ์‹คํ–‰์„ ๋ฏธ๋ฃจ๊ณ  ํ•ด๋‹น ์ด๋ฒคํŠธ ์‹œ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ ค๋ฉด ๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•˜์—ฌ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.
๐Ÿ”ฅํ•จ์ˆ˜(); -> ํ•จ์ˆ˜;

โœ…์ด๋ฒคํŠธ ๋“ฑ๋ก ๋ฉ”์„œ๋“œ addEventListener()

addEventListener(์–ด๋–ค์ด๋ฒคํŠธ์ธ์ง€, ์‹คํ–‰ํ•  ํ•จ์ˆ˜);

ํ•จ์ˆ˜ ์ง€์ •

<input type=<"text" id="inp">
<button id="btn" onclick="func_alert()" ondblclick="func_alert_dbl()">๋ฒ„ํŠผ</button>
<script>
  const inpEl = document.getElementById('inp');
  function inputfocus(){
    console.log('input addEventListener ํฌ์ปค์‹ฑ๋จ');
  }
  inpEl.addEventListener('focus', inputfocus);
</script>

์ต๋ช…ํ•จ์ˆ˜๋กœ ๋ฐ”๋กœ ์ €์žฅ

<script>
  inpEl.addEventListener('focus', function(){
    console.log('addEventListener ์•ˆ์—์„œ function ๋ฐ”๋กœ ์„ ์–ธ');
  });
</script>

ํ™”์‚ดํ‘œํ•จ์ˆ˜๋กœ ๋ฐ”๋กœ ์ €์žฅ

<script>
inpEl.addEventListener('focus', ()=>{
    console.log('addEventListener ์•ˆ์—์„œ function ๋ฐ”๋กœ ์„ ์–ธ');
    });
</script>

โ†“addEventListener()


๐Ÿ“–์ด๋ฒคํŠธ ๊ฐ์ฒด ์‚ฌ์šฉ

โœ…์ด๋ฒคํŠธ ๊ฐ์ฒด
์ด๋ฒคํŠธ ๊ฐ์ฒด๋ž€ ์ด๋ฒคํŠธ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ์ „๋‹ฌ๋˜๋Š” ์ด๋ฒคํŠธ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๊ฐ์ฒด์ด๋‹ค.

โœ…pointerEvent ๊ฐ์ฒด์˜ ์ฃผ์š” ์†์„ฑ

clientX ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ x์ขŒํ‘œโ€ƒ์ˆ˜ํ‰ ์Šคํฌ๋กค ํฌํ•จ X
clientY ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ y์ขŒํ‘œโ€ƒ์ˆ˜์ง ์Šคํฌ๋กค ํฌํ•จ X
pageX ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ x์ขŒํ‘œโ€ƒ์ˆ˜ํ‰ ์Šคํฌ๋กค ํฌํ•จ O
pageY ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ y์ขŒํ‘œโ€ƒ์ˆ˜์ง ์Šคํฌ๋กค ํฌํ•จ O
screenX ๋ชจ๋‹ˆํ„ฐ์˜ ์™ผ์ชฝ ์œ„ ๋ชจ์„œ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ x์ขŒํ‘œ
screenY ๋ชจ๋‹ˆํ„ฐ์˜ ์™ผ์ชฝ ์œ„ ๋ชจ์„œ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋œ y์ขŒํ‘œ
<body> 
  <div class="box" style="width:300px; height:300px; border:1px solid red;"></div>
  <script>
	  document.querySelector('.box').addEventListener('click', function(e){
    												//*e:๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์“ฐ๊ฒ ๋‹ค!
        console.log('clientX ' + e.clientX);
        console.log('clientY ' + e.clientY);
        console.log('pageX ' + e.pageX);
        console.log('pageY ' + e.pageY);
        console.log('screenX ' + e.screenX);
        console.log('screenY ' + e.screenY);
      })
  </script>
</body>

*e: ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์“ฐ๊ฒ ๋‹ค!
-> ์ด๋ ‡๊ฒŒ ๋“ฑ๋ก์„ ํ•ด๋‘๋ฉด ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰์‹œ์ผœ์ค€๋‹ค. ์–ด๋–ค ๋งˆ์šฐ์Šค์ขŒํ‘œ(or ํ‚ค๋ณด๋“œ)๊ฐ€ ๋ˆŒ๋ ธ๋Š”์ง€์™€ ๊ฐ™์€ ์ด๋ฒคํŠธ์— ๊ด€ํ•œ ์ •๋ณด๋„ ๊ฐ™์ด ์ „๋‹ฌํ•ด์ค€๋‹ค.


โœ…KeyboardEvent ๊ฐ์ฒด์˜ ์ฃผ์š” ์†์„ฑ

keyCode ํ‚ค๋ณด๋“œ์—์„œ ๋ˆŒ๋ฆฐ ํ‚ค์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’ ๋ฐ˜ํ™˜
ctrlKey Ctrl ํ‚ค๊ฐ€ ๋ˆŒ๋ ธ์œผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false ๋ฐ˜ํ™˜
altKey Alt ํ‚ค๊ฐ€ ๋ˆŒ๋ ธ์œผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false ๋ฐ˜ํ™˜
shiftKey Shift ํ‚ค๊ฐ€ ๋ˆŒ๋ ธ์œผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false ๋ฐ˜ํ™˜
<body>
  <input type="text">

  <script>
    const inp = document.querySelector('input');                        
    inp.addEventListener('keydown', function(e){
      console.log(e.keyCode);
      console.log(e.ctrlKey);
      console.log(e.altKey);
      console.log(e.shiftKey);
    });
  </script>
</body>

โ†“Ctrl ์ž…๋ ฅ ์‹œ

-> ํ‚ค์ฝ”๋“œ๋Š” 17์„ ๋ฐ˜ํ™˜, ctrlKey ์†์„ฑ์— true๋ฅผ ๋ฐ˜ํ™˜

โ†“Alt ์ž…๋ ฅ ์‹œ

-> ํ‚ค์ฝ”๋“œ๋Š” 18์„ ๋ฐ˜ํ™˜, altKey ์†์„ฑ์— true๋ฅผ ๋ฐ˜ํ™˜

โ†“Shift ์ž…๋ ฅ ์‹œ

-> ํ‚ค์ฝ”๋“œ๋Š” 16์„ ๋ฐ˜ํ™˜, shiftKey ์†์„ฑ์— true๋ฅผ ๋ฐ˜ํ™˜


๐Ÿ“–์ด๋ฒคํŠธ ์ทจ์†Œ

  • preventDefault() : ํƒœ๊ทธ์— ์ ์šฉ๋œ ๊ธฐ๋ณธ ์ด๋ฒคํŠธ๋ฅผ ์ทจ์†Œ

๐Ÿ“–this ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ

  • this ํ‚ค์›Œ๋“œ : ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ด
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์ผ ๋•Œ๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ target ์†์„ฑ์œผ๋กœ ์ฐธ์กฐ

CSS

.box {
      width: 100px;
      height: 100px;
      border: 1px solid black;
      margin: 10px 0px;
    }

HTML

<div id="box1" class="box"></div>
<div id="box2" class="box"></div>
<div id="box3" class="box"></div>

JS

const boxes = document.querySelectorAll('.box');
for(let box of boxes){
  box.addEventListener('click', function(){
    console.log(this);
    this.style.backgroundColor = 'red';
  });
}

๐Ÿ”ฝ์‹คํ–‰ ๊ฒฐ๊ณผ

-> this๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ ๊ฐ ๋ฐ•์Šค๋ณ„๋กœ ์•„์ด๋””๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ”ฅ์ถ”๊ฐ€) ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ ์‹œ!

const boxes = document.querySelectorAll('.box');

for(let box of boxes){
  box.addEventListener('click', (this)=>{
    console.log(this);
    this.style.backgroundColor = 'red';
  });
}

-> ์—๋Ÿฌ ๋ฐœ์ƒ!
why? ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—๋Š” this๊ฐ€ ์—†๋‹ค! function์œผ๋กœ ์„ ์–ธํ•œ ํ•จ์ˆ˜๋Š” this๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ๊ฐ’์„ ์ง€์ •ํ•˜์ง„ ์•Š๋Š”๋ฐ, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ์„ ์–ธํ•œ ํ•จ์ˆ˜์—๋Š” this๊ฐ€ ์—†๋‹ค. this ์ž์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋‹ค๋ณด๋‹ˆ ์ƒ์œ„ ํ™˜๊ฒฝ์˜ this๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค.
์œ„์˜ ๊ฒฝ์šฐ์—๋Š” this๊ฐ€ Window์— ๊ฑธ๋ ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ์ด ๋•Œ๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๊ด„ํ˜ธ ์•ˆ์— ์ด๋ฒคํŠธ์— ํ•ด๋‹นํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , ์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ target ์†์„ฑ์œผ๋กœ ์ ‘๊ทผํ•œ๋‹ค.

const boxes = document.querySelectorAll('.box');

for(let box of boxes){
  box.addEventListener('click', (e)=>{
    console.log(e.target);
    e.target.style.backgroundColor = 'red';
  });
}

๐Ÿ”ฝ์‹คํ–‰ ๊ฒฐ๊ณผ


๐Ÿ”ฅ์—ฐ์Šต๋ฌธ์ œ

์ฃผ์–ด์ง„ ๊ธฐ๋ณธ ์š”์†Œ๋“ค.

<input type="text" id="loginId" placeholder="์•„์ด๋””"><br>
<input type="password" id="loginPw" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ"><br>
<button id="btnLogin" onclick="tryLogin()">๋กœ๊ทธ์ธ ์‹œ๋„</button><br>

*๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด.
์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ์— ์žˆ๋Š” ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ๋ฅผ ์—†์• ๊ธฐ

*์•„์ด๋”” ์ž…๋ ฅ์นธ์—์„œ ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ์‹œ,
๋ฌด์Šจ ํ‚ค๊ฐ€ ๋ˆŒ๋ ธ๋Š”์ง€ ํ™•์ธ ํ•ด๋ณด๊ณ  ์—”ํ„ฐ๊ฐ€ ๋ˆŒ๋ฆฌ๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ์นธ์œผ๋กœ ์ด๋™

*๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ์นธ์—์„œ ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ์‹œ,
์—”ํ„ฐ๊ฐ€ ๋ˆŒ๋ฆฌ๋ฉด ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰

JS

const elId = document.querySelector('#loginId');
const elPw = document.querySelector('#loginPw');
const btn = document.querySelector('button');
elId.addEventListener('keydown', function(e){
  if(e.keyCode == 13){
    elPw.focus();
    console.log('์•„์ด๋”” ์ž…๋ ฅ');
  }
});
elPw.addEventListener('keydown', function(e){
  if(e.keyCode == 13){
    console.log('๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ');
    tryLogin();
  }
});
function tryLogin(){
  elId.value = '';
  elPw.value = '';
  console.log('๋กœ๊ทธ์ธ ์‹œ๋„ ๋ˆ„๋ฆ„');
}

๐Ÿ”ฝ์‹คํ–‰ ๊ฒฐ๊ณผ(enter ๋ˆ„๋ฆ„)

profile
Have a good day โŒฏโ€™โ–พโ€™โŒฏ

0๊ฐœ์˜ ๋Œ“๊ธ€