jQuery ๊ธฐ๋ณธ Event
.ready(function)
HTML๋ฌธ์์ ๋ชจ๋ DOM์์๋ค์ด ์๋ฒฝํ๊ฒ ์ฌ์ฉํ ์ค๋น๊ฐ ๋๋ฉด ํธ์ถ๋์ด functionํจ์๊ฐ ์คํ๋๋ค. ํ๋ผ๋ฏธํฐ ์์ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ jQuery ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ค.
์ธ๋ถ ๋ฆฌ์์ค๋ ์ด๋ฏธ์ง์ ์๊ด์์ด ๋ชจ๋ ๋ ์์๋ค์ด ์ค๋น๋๋ฉด ๋ฐ๋ก ํธ์ถ๋๊ธฐ ๋๋ฌธ์ window.onload
๋ณด๋ค ๋น ๋ฅด๋ค.
.on(events[,selector][,data],function)
.on() ๋ฉ์๋๋ .bind() ๋ฉ์๋์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ๋ฑ๋กํ๊ธฐ ์ํ์ฌ ์ฌ์ฉ๋๋ ๋ฉ์๋์ด๋ค. .on() ๋ฉ์๋๋ ๋์ ์ผ๋ก ์์ฑ๋ ์์์ ๋ํด์๋ ์ด๋ฒคํธ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค.
<body>
<button id="create">new</button>
<div></div>
</body>
$(function (){
$("#create").on("click",function(){
$("div").html("<button id='newButton'>ok</button>");
});
$("body").on("click","#newButton", function(event){
console.log("click");
});
});
.one(events[,selector][,data],function)
.one() ๋ฉ์๋๋ .on() ๋ฉ์๋์ ๋์ผํ ๊ธฐ๋ฅ์ ์ํํ๋ค. ์ฐจ์ด์ ์ .one()๋ฉ์๋๋ ๋ฉ์๋ ์ด๋ฆ์ฒ๋ผ ๋จ ํ๋ฒ๋ง ์ด๋ฒคํธ๊ฐ ์ฒ๋ฆฌ๋๋ฉฐ ์๋์ผ๋ก ์ด๋ฒคํธ ํธ๋ค๋ฌ๊ฐ ์ ๊ฑฐ(unbound)๋๋ค. .on()๋ฉ์๋๋ก ์ค์ ํ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์ ๊ฑฐํ๋ ๋ฐฉ๋ฒ์ ๋ช ์์ ์ผ๋ก .off(event) ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ๋๋๋ฐ, .one() ๋ฉ์๋๋ ์ด๋ฒคํธ ์คํ ํ์ ์๋์ผ๋ก ์ด๋ฒคํธ๊ฐ ํด์ ๋๋ค.
<body>
<button id="pressMe">Press Me</button>
</body>
$(function (){
$("#pressMe").one("click", function(){
console.log("๋จ ํ๋ฒ๋ง ์ด๋ฒคํธ ์คํ");
});
});
.trigger(eventType[,extraParameters])
์ด๋ฒคํธ๊ฐ ๋ฐ์๋ ๋ ์คํ๋ ํจ์๋ .on()๋ฉ์๋๋ก ์ฐ๊ฒฐ๋ ์ด๋ค ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ๊ฐ์ ์ ์ผ๋ก ์คํ์ํค๋ ๋ฉ์๋์ด๋ค. ๋ฐ๋ผ์ .trigger() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ฌ์ฉ์๊ฐ ์คํ์ํค๋ ์ด๋ฒคํธ์ ์์๋ฅผ ์ง์ ํด์ ์คํ์ํฌ ์ ์๋ค.
$(function (){
$("#foo").on("click", function(){
console.log($(this).text());
});
$("#foo").trigger("click");
});
.on()์ ํน์ ๋์ ์ด๋ฒคํธ ๋ฐ์ํ ๋ ๋์ํ ํจ์๋ฅผ ์ง์ ํ๊ณ , .trigger()๋ ํด๋ฆญ์ด๋ฒคํธ๋ฅผ ๊ฐ์ ๋ก ์คํํ๋ค. ๋ธ๋ผ์ฐ์ ๋ฅผ ์๋ก๊ณ ์นจํ๋ฉด ํด๋ฆญ์ด๋ฒคํธ ๋ฐ์ ์์ด๋ ์ฝ์์ ํ ์คํธ๊ฐ ์๋ ์ถ๋ ฅ๋๊ณ ์๋ค.
์ฌ๋ฌ๊ฐ์ ๋์ ํธ๋ฆฌ๊ฑฐ๋ฅผ ์ฌ์ฉํด์ ์์๋ฅผ ์ ํด์ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํค๊ฑฐ๋ ๊ฐ์ ๋ฐ์ํ๋๋ก ๋ง๋ค ์ ์๋ค.
<body>
<input type="text" name="message" id="message">
<button>Press</button>
</body>
$(function (){
$("button").on("click", function(){
$("[name=message]").trigger("myCustom");
});
$("[name=message]").on("myCustom", function(event){
$(this).val("๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์");
});
$("[name=message]").on("focus",function(event){
$(this).val("");
});
});
.off(events[,selector][,function])
on() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฑ๋ก๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์ ๊ฑฐ์ํค๋ ๋ฉ์๋์ด๋ค.
.unbind()์๋ ๋ค๋ฅด๊ฒ .off()๋ฉ์๋๋ ๋ ๋ฒ์งธ ์ธ์์ธ selector์ ํํฐ๋งํ ์ ์๋ ๊ฐ์ ์ค์ ํ ์ ์์ด์ ํจ์ฌ ์ธ๋ฐํ๊ฒ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์ ๊ฑฐ ์ํฌ ์ ์๋ค. ํ์ง๋ง ๋ฐ๋์ .on()๋ฉ์๋์์ ์ฌ์ฉํ๋ ์ธ์์ ๋์ผํด์ผ ๋๋ค.
<button id="on">on</button>
<button id="off">off</button>
$(function (){
$("#on").on("click",function(){
console.log("on~");
});
$("#off").on("click", function(){
$("#on").off("click");
})
});
jQuery ํผ Event
.focus([function]) ๋ฐ .blur([function])
.focus([function]) ๋ฉ์๋๋ ๋์ ์์์ focus ์ด๋ฒคํธ๊ฐ ๋ฐ์๋๋ฉด function์ด ์คํ๋๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ .on("focus",function)
์ ๋์ผํ๋ฉฐ ์ธ์ ์๋ .focus() ๋ฉ์๋๋ .trigger("focus")
๋ฅผ ์ค์ฌ์ ํํํ ๊ฒ์ด๋ค.
๋ฐ๋๋ก .blur(function) ๋ฉ์๋๋ ๋์ ์์๊ฐ focus๋ฅผ ์์์ ๋ ์คํ๋๋ค.
<body>
<form>
<input id="target" type="text">
<input type="text">
</form>
<button id="other">focus</button>
<button id="other2">blur</button>
</body>
$(function (){
$("#target").focus(function(){
$(this).css("background", "yellow");
});
$("#target").blur(function(){
$(this).css("background", "");
});
$("#other").on("click",function(){
$("#target").focus();
});
$("other2").on("click",function(){
$("#target").blur();
})
});
.change([function])
๋์ ์์์ change์ด๋ฒคํธ๊ฐ ๋ฐ์๋๋ฉด function์ด ์คํ๋๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ .on("change",function)
์ ๋์ผํ๋ฉฐ ์ธ์ ์๋ .change()๋ฉ์๋๋ trigger("change")
๋ฅผ ์ค์ฌ์ ํํํ ๊ฒ์ด๋ค.
change ์ด๋ฒคํธ๋ ๊ฐ์ด ๋ณ๊ฒฝ๋์์ ๊ฒฝ์ฐ์ ๋ฐ์๋๋ ์ด๋ฒคํธ๋ก์, <input>์์ ๋ฐ <textarea>์ <select>์์์์๋ง ์ ํ์ ์ผ๋ก ๋ฐ์๋๋ ์ด๋ฒคํธ์ด๋ค.
<body>
<select name="sweets">
<option>์ด์ฝ๋ฆฟ</option>
<option>์บ๋</option>
<option selected="selected">์ฌํ</option>
<option>์์ด์คํฌ๋ฆผ</option>
<option>์ฟ ํค</option>
</select>
<div></div>
</body>
$(function (){
$("select").change(function(){
$("div").text($("option:selected").text());
});
});
. submit([function])
๋์ ์์์ submit์ด๋ฒคํธ๊ฐ ๋ฐ์๋๋ฉด function์ด ์คํ๋๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ .on("submit",function)
๊ณผ ๋์ผํ๋ฉฐ ์ธ์ ์๋ .submit()๋ฉ์๋๋ .trigger("submit")
๋ฅผ ์ค์ฌ์ ํํํ ๊ฒ์ด๋ค.
submit ์ด๋ฒคํธ๋ ์ฌ์ฉ์๊ฐ form์ submitํ ๋๋ง ๋ฐ์๋๋ ์ด๋ฒคํธ๋ก์ ๋ช
์์ ์ผ๋ก <input type="submit">
์์ ์ <button type="submit">
์ ํด๋ฆญํ๊ฑฐ๋ ํผ์ ์์์์ Enter ํค๋ฅผ ๋๋ ์ ๋ ์ ํ์ ์ผ๋ก ๋ฐ์๋๋ ์ด๋ฒคํธ์ด๋ค.
<body>
ok์
๋ ฅํ๋ฉด '์ฑ๊ณต' ์ถ๋ ฅํ๊ณ ์๋๋ฉด '์คํจ' ์ถ๋ ฅ
<form id="target">
<input type="text">
<input type="submit" value="Go">
</form>
<div></div>
</body>
$("#target").submit(function(event){
var mesg = "์คํจ"
if($(":text").val()=='ok'){
mesg = "์ฑ๊ณต";
}
$("div").text(mesg);
event.preventDefault();
});
. keydown([function])
๋์ ์์์ keydown์ด๋ฒคํธ๊ฐ ๋ฐ์๋๋ฉด function์ด ์คํ๋๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ .on("keydown",function)
๊ณผ ๋์ผํ๋ฉฐ ์ธ์ ์๋ .keydown()๋ฉ์๋๋ .trigger("keydown")๋ฅผ ์ค์ฌ์ ํํํ ๊ฒ์ด๋ค.keydown ์ด๋ฒคํธ๋ ์ฌ์ฉ์๊ฐ ํค๋ณด๋๋ฅผ ๋๋ฅด๋ ์๊ฐ์ ๋ฐ์๋๋ค.
<body>
<form>
<input type="text" id="target">
</form>
<div></div>
</body>
$(function (){
$("#target").keydown(function(){
$("div").text(event.keyCode);
})
});
. keyup([function])
๋์ ์์์ keyup์ด๋ฒคํธ๊ฐ ๋ฐ์๋๋ฉด function์ด ์คํ๋๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ .on("keyup",function)๊ณผ ๋์ผํ๋ฉฐ ์ธ์ ์๋ .keyup()๋ฉ์๋๋ .trigger("keyup")
๋ฅผ ์ค์ฌ์ ํํํ ๊ฒ์ด๋ค. keyup ์ด๋ฒคํธ๋ ์ฌ์ฉ์๊ฐ ํค๋ณด๋๋ฅผ ๋๋ ๋ค ๋ผ๋ ์๊ฐ์ ๋ฐ์๋๋ค.
<form>
<input id="target" type="text">
</form>
<div></div>
$(function (){
$("#target").keyup(function(){
$("div").text($(":text").val().length);
});
});
jQuery ๋ง์ฐ์ค Event
. click([function])
๋์ ์์์ click ์ด๋ฒคํธ๊ฐ ๋ฐ์๋๋ฉด function์ด ์คํ๋๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ .on("click",function)์ ๋์ผํ๋ฉฐ ์ธ์ ์๋ .click() ๋ฉ์๋๋ .trigger("click")
๋ฅผ ์ค์ฌ์ ํํํ ๊ฒ์ด๋ค.
click ์ด๋ฒคํธ๋ ๋์ ์์๋ฅผ ๋ง์ฐ์ค ํฌ์ธํฐ๋ก ๋๋ฅด๊ณ ๋จ์ด์ง ๋ ๋ฐ์๋๋ ์ด๋ฒคํธ๋ก์, HTML์ ์ด๋ค ์์๋ผ๋ ์ด ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํฌ ์ ์๋ค.
$(function (){
$("p").click(function(){
$(this).slideUp();
})
});
.hover(functionIn, functionOut)
๋์ ์์์ mouseenter ์ mouseleave ์ด๋ฒคํธ๊ฐ ๋ฐ์๋๋ฉด ๊ฐ๊ฐ functionIn๊ณผ functionOut์ด ์คํ๋๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ $(selector).mouseenter(functionIn).mouseleave(functionOut)
๋ฉ์๋๋ฅผ ์ค์ฌ์ ํํํ ๊ฒ์ด๋ค.
<body>
<li>aaa</li>
<li>aaa</li>
<li class="fade">aaa</li>
<li class="fade">aaa</li>
</body>
$(function (){
$("li").hover(
function(){
$(this).append($("<span>***</span>"));
}, function(){
$(this).find("span").remove();
}
)
});
jQuery Ajax ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ฐฉ์
‘Asynchronous Javascript and XML’ ์ ์ฝ์๋ก์ ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ ํ๋ ์๋ก์ด ์น ํต์ ๋ฐฉ์์ ์๋ฏธํ๋ค.
์๋ฒ๊ฐ ์ค๊ฐ์ ajax ์์ง์ ์์ฒญํ๊ณ ์์ง์ด http ์์ฒญ์ ๋ณด๋ด์ค๋ค. ๋ณด๋ด์ฃผ๊ณ ์๋ ๋์์๋ ์ฌ์ฉ์๋ ๋ธ๋ผ์ฐ์ ๋ฅผ ๊ณ์ ์ฌ์ฉํ ์ ์๋ค. ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ ์ฌ์ฉ์์๊ฒ html๋ฐ์ดํฐ๊ฐ ์๋ xml ๋ฐ์ดํฐ๋ก ์์ง์๊ฒ ๋ณด๋ด์ฃผ๊ณ ์์ง์ html๋ฐ์ดํฐ๋ก ๋ฐ๊ฟ์ ์ฌ์ฉ์์๊ฒ ๋ณด๋ด์ค๋ค.
๊ธฐ์กด๋ฐฉ์ ์น์ฒ๋ฆฌ
์ฌ์ฉ์๊ฐ ์์ฒญ์ ๋ณด๋ด๋ฉด ์๋ต์ด ์ฌ๋๊น์ง ๋๊ธฐ์ํ๋ค.
ajax ์ฒ๋ฆฌ๋ฐฉ์
์ค๊ฐ ์์ง์ด ์ฌ์ฉ์ ์์ฒญ์ ๋ฐ๊ณ ์๋ต์ ์ฌ์ฉ์ ๋์ ๊ธฐ๋ค๋ ค์ค๋ค. ์ฌ์ฉ์๋ ๊ณ์ ์น๋ธ๋ผ์ฐ์ ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋ฐ๋ผ์ Ajax๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ด์ง ์นํ์ด์ง ์ ์ฒด๊ฐ ์๋ ์ผ๋ถ๋ถ์ ๋ํ ํ๋ฉด ๊ฐฑ์ ๋ฐ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ฉฐ ์๋ฒ์ ํต์ ์ค์๋ ๋ค๋ฅธ ์ผ์ ์ํํ ์ ์๋ค. ์ด๊ฒ์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ผ๊ณ ํ๋ค. ๋น๋๊ธฐ์ฒ๋ฆฌ๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๊ตฌํ๋์ง๋ง ๊ฐ๋จํ ์ฝ๋ ์์ฑ์ ์ํด ์ ์ด์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ค. ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์ ์ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํด์ XML Http Request(XHR)๊ฐ ์์ฒญ์ ๋ฐ๊ณ ์๋ฒ์ ์์ฒญ์ ๋ณด๋ธ๋ค. ๋ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ณ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๋ ์์ ์ ํ๋ค.
XML Http Request ๊ฐ์ฒด์ ํต์ ์ํ ํ์ธ ํ๋กํผํฐ
readyState ํ๋กํผํฐ
์ฌ์ฉ์์ ์๋ฒ์ ํต์ ์ด ๋๋ฐ๋ก ์ด๋ฃจ์ด์ก๋ ์ํ๊ฐ์ ์ ์ฅํ๋ค.
๋๋ฐ๋ก ์๋ต์ ๋ฐ์์์ ๊ฒฝ์ฐ ๋ฐํ๊ฐ์ 4๊ฐ ๋๋ค.
status ํ๋กํผํฐ
๋ฐํ๊ฐ 200์ ์ฒ๋ฆฌ ์ฑ๊ณต, 400๋๋ ํด๋ผ์ด์ธํธ ์์ฒญ ์๋ฌ, 500๋๋ ์๋ฒ ๋ด๋ถ ์๋ฌ๋ฅผ ์๋ฏธํ๋ค.
.jQuery.ajax( url[,settings] )
๋น๋๊ธฐ HTTP(Ajax) ์์ฒญ์ ์ํํ๋ ๋ฉ์๋์ด๋ค. url์ ์์ฒญ์ ๋ณด๋ผ ํ๊ฒ URL๊ฐ์ผ๋ก์ ๋ฌธ์์ด๋ก ์ง์ ํ๋ฉด ๋๊ณ settings์ key/value ์์ผ๋ก ๊ตฌ์ฑ๋, Ajax ์์ฒญ์ ์ถ๊ฐ๋ก ์ค์ ํ ์ ์๋ ์ค์ ๊ฐ์ด๋ค.
$(function () {
$("button").on("click", getData);
});
function getData() {
$.ajax({
type:"get", //์ ์กํ์
url:"sample01_text.jsp",//์๋ฒ์์ฒญ๋์ํ์ผ
dataType:"text", //์๋ตํ์
success: function (data, status, xhr) {
$("#result").text(data);
},
error: function (xhr, status, e) {
console.log("error", e);
console.log("status", status);
}
});
}
<body>
<button id="a">ajax์์ฒญ</button>
<div id="result"></div>
</body>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% String name = "ํ๊ธธ๋"; %>
<%= name %>
ajax ์ฝ๋ ์์ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋ฐ์ดํฐ ๊ฐ์ ํค-๋ฐธ๋ฅ๋ก ์ ๋ ฅํ๋ค. ์์ฒญ์ด ์ค๋ฉด ์์ง์ด ์์ฒญ๋ด์ฉ์ ์๋ฒ๋ก ๋ณด๋ด์ฃผ๊ณ success๋ถํฐ๋ ์๋ต ์ฒ๋ฆฌ ๋ด์ฉ์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํ ํจ์๋ฅผ ์์ฑํ๋ค.
์ฒ๋ฆฌ ์ฑ๊ณต์ ์คํ๋๋ ํจ์๋ ์๋ต๋ฐ์ ๋ฐ์ดํฐ, ์ํ, ์์ฒญ๊ฐ์ฒด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ๋๋ค. ๊ฒฐ๊ณผ ๋ถ๋ถ์ ์๋ต๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ์ฐ์ด์ฃผ๊ณ ์๋ค. ์๋ฌ ๋ฐ์์ ์๋ฌ ํจ์๋ฅผ ํธ์ถํ๋ฉฐ, ์ฑ๊ณตํจ์์ ๋งค๊ฐ๋ณ์ ์์๊ฐ ๋ค๋ฅด๋ค. ์๋ฌ์ฝ๋์๋ ๋ณดํต ์๋ฌ๋ฉ์ธ์ง๊ฐ ๋ด๊ฒจ์๋ค.
์ฐ์ต 1
<body>
<button id="a">ajax์์ฒญ</button>
v1:<input type="text" name="v1" id="v1"><br>
v2:<input type="text" name="v2" id="v2"><br>
<div id="result"></div>
</body>
$(function () {
$("button").on("click", getData);
});
function getData() {
$.ajax({
type:"get", //์ ์กํ์
url:"sample01_text2.jsp",//์๋ฒ์์ฒญ๋์ํ์ผ
data:{
v1: $("#v1").val(),
v2: $("#v2").val()
},
dataType:"text", //์๋ตํ์
success: function (data, status, xhr) {
$("#result").text(data);
},
error: function (xhr, status, e) {
$("#result").append(error);
$("#result").append(status);
}
});
}
<%
String v1 = request.getParameter("v1");
String v2 = request.getParameter("v2");
System.out.print(v1+"\t"+v2);
%>
<%= v1 + ":"+v2+"์ ์ก์๋ฃ" %>
๊ฐ๋จํ ๋ฐ์ดํฐ ์ ์ก
์ฐ์ต 2
<body>
<button id="a">ajax์์ฒญ</button>
<div id="result"></div>
</body>
$(function () {
$("#a").on("click", getData);
});
function getData() {
$.ajax({
type:"get",
url:"sample02_json.jsp",
dataType: "json",
success:function(data,status,xhr){
var username = data.username;
var age = data.age;
console.log(username, age);
$("#result").text(username+":"+age);
},
error: function(xhr, status, error){
$("#result").text(error);
}
})
}
<%
String name = "ํ๊ธธ๋";
int age = 20;
%>
{
"username":"<%= name %>",
"age":"<%= age %>"
}
json ๋ฐ์ดํฐ ๋ง๋ค๊ธฐ