์ด๋ฒ ์๊ฐ์ jQuery๋ฅผ ์์ํด ๋ณผ ๊ฒ์ด๋ค. ์ ์ด์ฟผ๋ฆฌ๋ ๋ฌด์์ผ๊น?
jQuery๋ ๋น ๋ฅด๊ณ ์๊ณ ๊ธฐ๋ฅ์ด ํ๋ถํ Javascript์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค. ์ฌ๋ฌ ๋ธ๋ผ์ฐ์ ์์ ์๋ํ๊ณ ์ฌ์ฉํ๊ธฐ ์ฌ์ด API๋ฅผ ์ฌ์ฉํ์ฌ HTML ๋ฌธ์ ํ์ ๋ฐ ์กฐ์, ์ด๋ฒคํธ ์ฒ๋ฆฌ, ์ ๋๋ฉ์ด์ ๋ฐ Ajax์ ๊ฐ์ ์์ ์ ํจ์ฌ ๋ ๊ฐ๋จํ๊ฒ ๋ง๋ ๋ค. ๋ ๋ค๋ชฉ์ ์ฑ๊ณผ ํ์ฅ์ฑ์ ์กฐํฉ์ผ๋ก jQuery๋ ์๋ฐฑ๋ง ๋ช ์ ์ฌ๋๋ค์ด Javascript๋ฅผ ์์ฑํ๋ ๋ฐฉ์์ ๋ณํ์์ผฐ๋ค.
๋จผ์ jQuery ์ฌ์ดํธ์ ์ ์ํ๋ค.
์ ์์ ํ๊ฒ ๋๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ฐฝ์ด ๋จ๋๋ฐ ์ฐ์ธก ์ค๊ฐ์ฏค ๋ณด๋ฉด "Download jQuery"๋ผ๋ ์ฃผํฉ์ ๋ฒํผ์ ํด๋ฆญํ๋ค.
๋นจ๊ฐ ๋ฐ์ค์ ์๋ ํญ๋ชฉ์ ์ ํํ๋ฉด ๋ ๋ค์๊ณผ ๊ฐ์ ์ฐฝ์ด ๋ฌ๋ค.
์ด ์ฐฝ์ด ๋จ๋ฉด ์ฐฝ์ ํด๋ฆญํ๊ณ Ctrl+s
๋ฅผ ๋๋ฌ ์ ์ฅํ๋ค.
์ ์ฅํ ํ์ผ์ ์์ ํ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํด๋์ ๊ฐ์ด ์ ์ฅํ๋ค.
๊ทธ๋ฆฌ๊ณ html
ํ์ผ์ body
ํ๊ทธ ์ ์ผ ๋ฐ์ ์ฐ๋ํ๋ค.
<script src="jquery-3.6.1.js"></script>
$(document).ready(function(){
$(":header").addClass("favorite");
$("#four").addClass("favorite");
$("#three,ul>li:nth-child(2)").addClass("favorite");
$("#one").removeClass("hot");
$("p:nth-child(even)").css("background-color","yellow");
$("#five").css({"background-color":"red","font-size":"2em"})
.css("border", "3px solid blue");
$("p:lt(3)").hide().fadeIn(4000); //์ง์ ๋ ์ธ๋ฑ์ค๋ณด๋ค ์์
$("p:gt(2)").hide().fadeIn(15000); //์ง์ ๋ ์ธ๋ฑ์ค๋ณด๋ค ํฐ
$("p:eq(2)").hide().fadeIn(15000); //์ง์ ๋ ์ธ๋ฑ์ค๋ง ์ ํ
$("*").hide().fadeIn(15000); //์ ์ฒด ์ ํ
$("button").on("click",clikMe);
function clikMe(){
$("#five").css({"background-color":"red","font-size":"2em"})
.css("border", "3px solid yellow");
}
$("h1").on("click",function(){
$("#five").css({"background-color":"red","font-size":"2em"})
.css("border", "3px solid blue");
});
});
๐ฑ ์ฝ๋๋ฆฌ๋ทฐ
$(document).ready(function(){ };
์ฒ์ jQuery๋ฅผ ์์ฑํ ๋ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ์ด๋ค.
$(document).ready()
๋ ๋ฌธ์๊ฐ ์ค๋น๋๋ฉด ๋งค๊ฐ๋ณ์๋ก ๋ฃ์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ๋ผ๋ ์๋ฏธ์ด๋ค.window.onload = function(){ };
๊ณ ์ ์ด๋ฒคํธ ๋ชจ๋ธ์ ํ ๋ฒ์ ํ๋์ ์ด๋ฒคํธ๋ง ์ฐ๊ฒฐํ ์ ์๋ค. ๋ฐ๋ฉด์ jQuery์ ์ด๋ฒคํธ ๋ฉ์๋๋ ํ์ค ์ด๋ฒคํธ ๋ชจ๋ธ์ด๋ ์ธํฐ๋ท ์ต์คํ๋ก๋ฌ ์ด๋ฒคํธ ๋ชจ๋ธ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ด๋ฒคํธ๋ก ์ฌ๋ฌ๊ฐ์ ํจ์๋ฅผ ์ฐ๊ฒฐ ํ ์ ์๋ค.
$(function(){};
$(document).ready()
๋ฉ์๋๋ ๊ต์ฅํ ๋ง์ด ์ฌ์ฉ๋๋ฏ๋ก jQuery์์๋ ๊ฐ๋จํ๊ฒ ์ฌ์ฉํ ์ ์๋ ํํ๋ฅผ ์ ๊ณตํฉ๋๋ค.$(":header").addClass("favorite");
":header"
๋<h1~h6>
ํ๊ทธ๋ฅผ ์ ๋ถ ์ ์ธํ๋ ๊ฒ์ด๋ค.
.addClass("favorite");
๋ ํด๋์ค๋ฅผ ์์ฑํ๋ ๊ฒ์ด๋ค.
๊ณง<h1~h6>
ํ๊ทธ๊ฐ ์๋ ํญ๋ชฉ์ ํด๋์ค๋ฅผ ์ถ๊ฐ์ํค๋ ๊ฒ์ด๋ค.$("#four").addClass("favorite");
("#four")
๋id
๊ฐfour
์ธ ํญ๋ชฉ์ ์ ์ธํ๋ค.
.addClass("favorite");
์id
๊ฐfour
์ธ ํญ๋ชฉ์favorite
ํด๋์ค๋ฅผ ์ถ๊ฐํ๋ค.$("#three,ul>li:nth-child(2)").addClass("favorite");
("#three,ul>li:nth-child(2)")
๋id
๊ฐthree
์ธ ํญ๋ชฉ๊ณผul
์๋์ ์๋li
2๋ฒ์งธ ํญ๋ชฉ์ ์ ์ธํ๋ค.
.addClass("favorite");
์id
๊ฐthree
์ธ ํญ๋ชฉ๊ณผ
ul
์๋์ ์๋li
2๋ฒ์งธ ํญ๋ชฉ์favorite
ํด๋์ค๋ฅผ ์ถ๊ฐํ๋ค.$("#one").removeClass("hot");
("#one")
์id
๊ฐone
์ธ ํญ๋ชฉ์ ์ ์ธํ๋ค.
.removeClass("hot");
์id
๊ฐone
์ธ ํญ๋ชฉ์hot
์ด๋ผ๋ ํด๋์ค๋ฅผ ์ญ์ ํ๋ค.$("p:nth-child(even)").css("background-color","yellow");
p
ํ๊ทธ์ ์ง์๋ฒ์งธ๋ฅผ ์ ์ธํ๊ณ.css("background-color","yellow");
css๋ก ์ง์ ํด ๋ฐฑ๊ทธ๋ผ์ด๋ ์์ yellow๋ก ๋ณ๊ฒฝํ๋ค.$("#five").css({"background-color":"red","font-size":"2em"}) .css("border", "3px solid blue");
css๋ฅผ ์ ์ฉํ๋ ๋ฉ์๋๋ฅผ ์ธ ๋ ํ๋์ฉ ์์ฑํ๊ธฐ๋ ์ฌ๋ฌ๋ฒ ์จ์ผํ๋ ๋ฒ๊ฑฐ๋ฌ์์ด ์๋ค. ๊ทธ๋ด๋
{}
๋ก ๋ฌถ๊ฑฐ๋ ์๋๋ฉด ์ ์ฝ๋์ ๊ฐ์ด.css
๋ฅผ ํ๋ฒ ๋ ์จ์ค๋ ๋๋ค.
โ ๋จ ์ฃผ์์ฌํญ์ด ์๋ค.$("#five").css({"background-color":"red","font-size":"2em"})
๋ค์;
์ด ๋ถ์ด์์ผ๋ฉด ์๋๋ค. ๋ถ์ด์์ง ์์์ผ ์ปดํจํฐ๊ฐ ์ด์ด์ ธ์๋ค๊ณ ํ๋จํด์์ด๋ค.$("p:lt(3)").hide().fadeIn(4000); //์ง์ ๋ ์ธ๋ฑ์ค๋ณด๋ค ์์
์๋ฅผ ๋ค์ด ์๋์ ๊ฐ์ด
p
ํ๊ทธ๊ฐ ์ฌ๋ฌ๊ฐ๊ฐ ์๋ค.<p>๋๋ ๋ฌธ๋จ์ ๋๋ค1.</p> <p>๋๋ ๋ฌธ๋จ์ ๋๋ค2.</p> <p>๋๋ ๋ฌธ๋จ์ ๋๋ค3.</p> <p>๋๋ ๋ฌธ๋จ์ ๋๋ค4.</p>
("p:lt(3)")
๋p
ํ๊ทธ์ ์ธ๋ฑ์ค ๋ฒํธ 3๋ฒ์งธ ๋ณด๋ค ์์ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ์ง์ ํ๋ค. ์ฆ ์ธ๋ฑ์ค 3๋ฒ์งธ ํญ๋ชฉ์ธ "๋๋ ๋ฌธ๋จ์ ๋๋ค4." ๋ณด๋ค ์์ ์ ํญ๋ชฉ๋ค์ด ์ ์ธ์ด๋๋ค. ๊ทธ๋ผ "๋๋ ๋ฌธ๋จ์ ๋๋ค1.", "๋๋ ๋ฌธ๋จ์ ๋๋ค2.", "๋๋ ๋ฌธ๋จ์ ๋๋ค3."์ด.hide().fadeIn(4000);
๋ ๊ฒ์ด๋ค.
.hide
๋ ์ ํ๋ ์์๋ฅผ ์ฌ๋ผ์ง๊ฒ ํ๋ ๋ฉ์๋์ด๊ณ.fadeIn(4000);
๋ 4์ด๊ฐ ์์ํ ๋ํ๋๊ฒ ํ๋ ๋ฉ์๋์ด๋ค.$("p:gt(2)").hide().fadeIn(15000); //์ง์ ๋ ์ธ๋ฑ์ค๋ณด๋ค ํฐ
์์ ๊ฐ์ ๋ฐฉ์์ด์ง๋ง
("p:gt(2)")
๋ ์ง์ ๋ ์ธ๋ฑ์ค๋ณด๋ค ํฐ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ์ง์ ํ๋ ๊ฒ์ด๋ค.
์ฆ 2๋ฒ์งธ ์ธ๋ฑ์ค์ธ "๋๋ ๋ฌธ๋จ์ ๋๋ค3." ๋ณด๋ค ํฐ "๋๋ ๋ฌธ๋จ์ ๋๋ค4."๊ฐ ์ง์ ๋๋ค.$("p:eq(2)").hide().fadeIn(15000); //์ง์ ๋ ์ธ๋ฑ์ค๋ง ์ ํ
์์ ๊ฐ์ ๋ฐฉ์์ด์ง๋ง
("p:eq(2)")
๋ ์ง์ ๋ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ์ง์ ํ๋ค.
์ฆ 2๋ฒ ์งธ ์ธ๋ฑ์ค์ธ "๋๋ ๋ฌธ๋จ์ ๋๋ค3."์ด ์ง์ ๋๋ค.$("*").hide().fadeIn(15000); //์ ์ฒด ์ ํ
("*")
์ ์ ์ฒด๋ฅผ ์ ํํด ์ ์ฒด์์์ ์ ์ฉ์ํจ๋ค.$("button").on("click",clikMe); function clikMe(){ $("#five").css({"background-color":"red","font-size":"2em"}) .css("border", "3px solid yellow"); }
$("button").on("click",clickMe);
button
์ ์ ์ธํ๊ณon(click, clickMe)
on
๋ฉ์๋์click
์ด๋ผ๋ ์ด๋ฒคํธ๋กclickMe
๋ผ๋ ํจ์๋ช ์ ์ ์ธํ๋ค.
function clikMe(){ $("#five").css({"background-color":"red","font-size":"2em"}) .css("border", "3px solid yellow"); }
ํจ์๋ฅผ ์ง์ ํด์ค๋ค. ์ฆbutton
์ ํด๋ฆญํ์ ๋id
๊ฐfive
์ธ ํญ๋ชฉ์ css๋ฅผ ์ง์ ํด์ฃผ๋ ๊ฒ์ด๋ค.$("h1").on("click",function(){ $("#five").css({"background-color":"red","font-size":"2em"}) .css("border", "3px solid blue"); });
h1
์ ์ง์ ํดon(click, function()
on
๋ฉ์๋์click
์ด๋ผ๋ ์ด๋ฒคํธ๋ก ๋ฐ๋ก ์ต๋ช ํจ์๋ฅผ ์ ์ธํ ๊ฒ์ด๋ค. ์ฆh1
์ ํด๋ฆญํ์ ๋h1
ํญ๋ชฉ์ css๋ฅผ ์ง์ ํด์ฃผ๋ ๊ฒ์ด๋ค.