-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_jquery_basic_4_1
jQuery๊ฐ ๊ฐ์ง ๊ธฐ๋ฅ ์ด์ธ์ ๊ธฐ๋ฅ์ด ํ์ํ ๊ฒฝ์ฐ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ฌ ๊ธฐ๋ฅ์ ํ์ฅํ๋ ๋ฐฉ๋ฒ
- ์ ํธ๋ฆฌํฐ
์ฝ๋ฉ์ ๋์์ ์ฃผ๋ ๊ธฐ๋ฅ๋ค์ ์๋ฏธํ๋ค.
์ฌ์ฉ๋ฒ์ ์ฃผ๋ก jQuery ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์๊ณ ๋ค์๊ณผ ๊ฐ์ด jQuery ํด๋์ค์ ์ง์ ์ ๊ทผํด์ ์ฌ์ฉํ๋ค.
jQuery.์ ํธ๋ฆฌํฐ();
$.์ ํธ๋ฆฌํฐ();- ํ๋ฌ๊ทธ์ธ
๋ ธ๋๋ฅผ ๋ค๋ฃจ๋ ํน์ ๊ธฐ๋ฅ์ ์ฌ์ฌ์ฉํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ ํฌ์ฅ ๊ธฐ๋ฅ์ด๋ค.
์ฌ์ฉ๋ฒ์ ์ฃผ๋ก ๋ค์๊ณผ ๊ฐ์ด jQuery ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ์ ์ฌ์ฉํ๋ค.
$("์ ํ์").ํ๋ฌ๊ทธ์ธ(์ต์
);
var $๊ฒฐ๊ณผ = $("์ ํ์");
$๊ฒฐ๊ณผ.ํ๋ฌ๊ทธ์ธ(์ต์
);jQuery ์ ํธ๋ฆฌํฐ๋ ๋จ์ํ jQuery์ ํด๋์ค ๋ฉ์๋์ด๋ค.
-
trim()
๋ฌธ์์ด์ ์์๊ณผ ๋์ ์๋ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ๋ค. -
index()
์ผ์นํ๋ ์์์ค์ ์ฃผ์ด์ง ์์๋ฅผ ์ฐพ๋๋ค. -
proxy()
ํจ์ ๋ด๋ถ์ this๋ฅผ ํน์ ๊ฐ์ฒด๋ก ๋ณํํ๋ค.this์์ ๋ฐ์ํ๋ ๋ฌธ์
Function.prototype.bind
JavaScript ํ๋ก์(Proxy) -
extend()
๋๊ฐ ์ด์์ ๊ฐ์ฒด์ ๋ด์ฉ์ ์ฒซ๋ฒ์งธ ๊ฐ์ฒด๋ก ๋ณํฉํ๋ค. -
data()
ํด๋น ์๋ฆฌ๋จผํธ์ Javascript Type์ value๋ฅผ ๋ก ์ ์ฅํ๊ฑฐ๋ ์ ์ฅ๋์ด์๋ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์จ๋ค.
ํน์ ๊ธฐ๋ฅ์ ์ฌ์ฌ์ฉํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ ํฌ์ฅ ๊ธฐ๋ฅ์ด๋ค.
ํ๋ฌ๊ทธ์ธ ๊ตฌ์กฐ
(function($) {
$.fn.ํ๋ฌ๊ทธ์ธ์ด๋ฆ = function(์์ฑ ๊ฐ) {
this.each(function(index) {
}
return this;
}
})(jQuery)jQuery ํ๋ฌ๊ทธ์ธ์ jQuery ํด๋์ค์ fn์ด๋ ๊ณณ์ ํ๋ฌ๊ทธ์ธ์ ๋ง๋ ๋ค.
์ฌ๊ธฐ์ fn์ prototype์ ์ค์ฌ ์ด ๋๋ค์์ผ ๋ฟ์ด๋ค.
jQuery ํ๋ฌ๊ทธ์ธ์ ๋ชจ๋ jQuery์ ์ธ์คํด์ค ๋ฉ์๋์ผ ๋ฟ์ด๋ค.
์ค์ต
each ์ค์ต
accordion ์ค์ต
-
find: ๊ฒ์๋ ๊ฒฐ๊ณผ ๊ฐ์ฒด ์งํฉ์ ๋ชจ๋ ํ์๋ ๋ฒจ(์์)์ ๊ฒ์ ํ ๋ ์ฌ์ฉ ํฉ๋๋ค.-
("A").find("B"): A ํ์์์ B๋ฅผ ์ฐพ์
-
-
filter: ๊ฒ์๋ ๊ฒฐ๊ณผ ๊ฐ์ฒด ์งํฉ์์ ํน์ ์งํฉ์ผ๋ก ๋ค์ ํ๋ฒ ๊ฒ์ ํ ๋ ์ฌ์ฉํฉ๋๋ค.-
("A").filter("B"): A ๋ด์์ B๋ฅผ ์ฐพ์
-
-
children: ๊ฒ์๋ ๊ฒฐ๊ณผ ๊ฐ์ฒด ์งํฉ ์ง์ ํ์๋ ๋ฒจ(์์)์ ๊ฒ์ ํ ๋ ์ฌ์ฉํฉ๋๋ค.-
("A").chlidren(): A ํ์์ ์์๋ค
-
๋งค๊ฐ๋ณ์๊ฐ ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ
var a = {opt1: 1, opt2: 1};
var b = {opt1: 2, opt3: 2};
var c = {opt1: 3};
var r = jQuery.extend(a, b, c);
console.log("A: opt1=" + a.opt1 + " opt2=" + a.opt2 + " opt3=" + a.opt3);
console.log("B: opt1=" + b.opt1 + " opt2=" + b.opt2 + " opt3=" + b.opt3);
console.log("C: opt1=" + c.opt1 + " opt2=" + c.opt2 + " opt3=" + c.opt3);
console.log("R: opt1=" + r.opt1 + " opt2=" + r.opt2 + " opt3=" + r.opt3);
console.log("A === R?: " + (a === r));"A: opt1=3 opt2=1 opt3=2"
"B: opt1=2 opt2=undefined opt3=2"
"C: opt1=3 opt2=undefined opt3=undefined"
"R: opt1=3 opt2=1 opt3=2"
"A === R?: true"
- ์ฒซ๋ฒ์งธ ๊ฐ์ฒด์ ๋๋ฒ์งธ ๊ฐ์ฒด์ ์์ฑ์ ์ถ๊ฐํ๋ค. ๋ง์ฝ, ์์ฑ์ด ์ด๋ฏธ ์กด์ฌํ๋ ๊ฒฝ์ฐ ๋ฎ์ด์ด๋ค.
- ์ฒซ๋ฒ์งธ ๊ฐ์ฒด์ ์์ฑ์ ์ถ๊ฐํ๋ ๊ฒ์ ์ ๋ฌ๋ ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด ๊ฐ์ ๋งํผ ์์ฐจ์ ์ผ๋ก ์งํํ๋ค.
- ์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐํํ๋ค.
๊ธฐ๋ณธ ์ต์ ์ฒ๋ฆฌ์ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋จ
// ๊ธฐ๋ณธ ์ต์
์ฒ๋ฆฌ
function foo(userOptions) {
var defaultOptions = {
foo: 2,
bar: 2
};
var allOptions = jQuery.extend(
defaultOptions,
userOptions
);
doSomething(allOptions);
}foo({foo:1}); // allOption === {foo:1, bar:2}
foo({foo:1, baz:1}); // allOption === {foo:1, bar:2, baz:1}์ฒซ๋ฒ์งธ ๊ฐ์ฒด๊ฐ ๋ณ๊ฒฝ๋์ง ์๊ฒ ํ๋ ๊ฒฝ์ฐ
var a = {foo: "1", bar: "2"};
var b = {foo: "2", baz: "3"};
var c = jQuery.extend({}, a, b);
console.log(c === a); // false
console.log(c); // opt1: "2", opt2: "2", opt3: "3" ๋งค๊ฐ๋ณ์๊ฐ ํ๊ฐ์ธ ๊ฒฝ์ฐ
jQuery.extend()์ ๋งค๊ฐ๋ณ์๊ฐ ํ๊ฐ์ธ ๊ฒฝ์ฐ jQuery ๊ฐ์ฒด ์์ฒด๊ฐ ์์ ๋ ๊ฐ์ฒด๋ก ๋๋ค.
console.log( "Before: " + jQuery.foo );
jQuery.extend({foo:1});
console.log( "After: " + jQuery.foo );
// Before: undefined
// After: 1