jQuery ์์๋ ์ด๋ฒคํธ๋ฅผ ๋ฑ๋กํ๊ณ ์คํํ ์ ์๋ค.
jQuery์์ ์ฌ์ฉ๋๋ ์ด๋ฒคํธ๋ ์ข
๋ฅ๊ฐ ๊ต์ฅํ ๋ง๋ค.
๋ง์ด ์ฌ์ฉ๋๋ ๋ช๊ฐ์ง
์ด ์ธ์ ๋ค๋ฅธ ์ด๋ฒคํธ๋ฅผ ์๊ณ ์ถ๋ค๋ฉด
API ์ฌ์ดํธ ์์ events ์นดํ
๊ณ ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.
' ์ด๋ฒคํธ ๋ฑ๋ก '
์ด๋ฒคํธ ๋ฑ๋ก ๋ฐฉ๋ฒ
1. $(selector).์ถ๊ฐํ ์ด๋ฒคํธ(์คํํ ํจ์)
2. $(selector).on("์ถ๊ฐํ ์ด๋ฒคํธ",์คํํ ํจ์)
1. $(selector).click(function(){});
- .click() : ์ถ๊ฐํ ์ด๋ฒคํธ
- function(){} : ์ด๋ฒคํธ๊ฐ ์คํ๋๋ฉด ์คํํ ํจ์
//hover = mouseenter + mouseleave
$("#overzone").hover(
function(){
$(this).css({"background-color":"red"});
},
function(){
$(this).css({"background-color":"green"});
}
);
=> id๊ฐ overzone ์ธ ์์์ ๋ง์ฐ์ค๋ฅผ ์ค๋ฒํ๋ฉด ์ด ์์(this) ์ css ๋ฅผ ์ ์ฉํ๋ค.
2. $(selector).on("click",function(){});
- on์ผ๋ก ์์ฑํ ์ด๋ฒคํธ๋ off๋ฅผ ํตํด ํค๊ณ ๋ ์ ์๋ค.
- "click" : ์ถ๊ฐํ ์ด๋ฒคํธ
- function(){} : ์ด๋ฒคํธ๊ฐ ์คํ๋๋ฉด ์คํํ ํจ์
$("p").on("dblclick",function(e){
$(this).css({'color':'blue'});
});
pํ๊ทธ์ธ ์์๋ฅผ ๋๋ธํด๋ฆญํ๋ฉด ์ด ์์(this)์ css๊ฐ ์ ์ฉ๋๋ค.
1) pํ๊ทธ์ธ ์์๊ฐ ์ฌ๋ฌ๊ฐ์ด๋ค๋ณด๋ ํด๋ฆญํ ์์๋ง ๋ฐ๋๋๋ก ํ๊ธฐ ์ํด์ 2๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
$("p").on("click",function(e){
console.log(e);
//$('p').css({'color':'orange'}); // ํด๋ฆญํ ์์๋ง ๋ฐ๋์ด์ผ ํ๋ค. -> 2๊ฐ์ง ๋ฐฉ๋ฒ
//1. e.target ์ด์ฉ **
//$(e.target).css({'color':'orange'});
//2. this๋ฅผ ์ด์ฉ *
$(this).css({'color':'orange'});
});
$('p').css({'color':'orange'}); ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋ pํ๊ทธ๊ฐ orange ์์ผ๋ก ๋ฐ๋๋ค.
1. e.target ์ด์ฉ
$(e.target).css({'color':'orange'});
2. this ์ด์ฉ
$(this).css({'color':'orange'});
2) on์ ์ด์ฉํด์ ์ฌ๋ฌ ์ด๋ฒคํธ ๋ฑ๋กํ๊ธฐ
$("#mousezone").on({
mouseenter:function(e){
$(this).html('๋ง์ฐ์ค๊ฐ ๋ค์ด๊ฐ ์ํ');
},
mouseleave:function(e){
$(this).html('๋ง์ฐ์ค๊ฐ ๋๊ฐ ์ํ');
},
mousedown:function(e){
$(this).html('๋ง์ฐ์ค๊ฐ ๋๋ฆฐ ์ํ');
},
mouseup:function(e){
$(this).html('๋ง์ฐ์ค๋ฒํผ ๋ ์ํ');
}
});
mouseenter : function(){}
์ด๋ฒคํธ์ข
๋ฅ : ์คํํ ํจ์
' ์ด๋ฒคํธ ์ญ์ '
on์ผ๋ก ์์ฑ๋ ์ด๋ฒคํธ๋ off๋ก ์ ๊ฑฐํ ์ ์๋ค.
- on์ผ๋ก ์ด๋ฒคํธ ๋ฑ๋ก
//$(selector).on("์ถ๊ฐํ ์ด๋ฒคํธ",์คํํ ํจ์)
$("#btn").on("click",function(){ // ์ต๋ช
ํจ์ or ์ฝ๋ฐฑํจ์๋ผ๊ณ ๋ถ๋ฅธ๋ค.
$("body").append("<button class='newBtn' onclick='clickEvt()'>click</button>");
$("body").append("<button class='newBtn2'>click2</button>");
});
- off๋ก ์ด๋ฒคํธ ์ญ์
// ์ด๋ฒคํธ ์ญ์ (on์ผ๋ก ์์ฑ๋ ์ด๋ฒคํธ๋ off๋ก ์ ๊ฑฐํ ์ ์๋ค.)
$('#evtDel').on('click',function(){
$('#btn').off('click');
});
์์ ์์ด๋๊ฐ btn ์ธ ์์('๋ฒํผ ์์ฑ' ๋ฒํผ)์ ํด๋ฆญ์ด๋ฒคํธ๋ฅผ ๋ฑ๋กํด๋จ๋๋ฐ
์์ด๋๊ฐ evtDel์ธ ์์('์ด๋ฒคํธ ์ญ์ ' ๋ฒํผ) ์ ํด๋ฆญํ๋ฉด '๋ฒํผ ์์ฑ' ๋ฒํผ์ ๋ฑ๋กํด๋จ๋ ์ด๋ฒคํธ๋ฅผ off๋ก ์ญ์ ํ๋ค.
'๋ฒํผ ์์ฑ' ๋ฒํผ์๋ ๋ฒํผ์ ๋๋ฅด๋ฉด click, click2 ๋ฒํผ์ด ์์ฑ๋๋ ์ด๋ฒคํธ๊ฐ ๋ฑ๋ก๋์ด ์๋๋ฐ
'์ด๋ฒคํธ ์ญ์ ' ๋ฒํผ์ ๋๋ฅด๋ฉด ์ด ์ด๋ฒคํธ๊ฐ ์ ๊ฑฐ๋์ด
์์ฑ ๋ฒํผ์ ๋๋ฌ๋ ๋์ด์ ๋ฒํผ์ด ์์ฑ๋์ง ์๋๋ค.
'Front-End > jQuery' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
jQuery : focus(), blur() ์ด๋ฒคํธ (0) | 2022.06.06 |
---|---|
jQuery ๋์ค์ ์์ฑ๋ ์์์ ์ด๋ฒคํธ ๋ฑ๋ก (0) | 2022.06.06 |
jQuery ๊ธฐ์ด ๋น ๋ฅด๊ฒ ํ๊ธฐ (0) | 2022.06.02 |