
์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋ ๋ง๋ค ์ด๋ค ์์ ์ ํ ์ง ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ก ์์ฑํ๋ ๊ฒ์ด๋ค.
ํด๋ฆญ
<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
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๋ฅผ ๋ฐํ
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 ๋๋ฆ)
