์ƒˆ์†Œ์‹

Languages/Java Script

[Js] jQuery ๊ฐ์ฒด | map() | jQuery API

  • -
๋ฐ˜์‘ํ˜•

๋ชจ๋“  ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ [์ƒํ™œ์ฝ”๋”ฉ] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์˜์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "egoing Lee"๋‹˜์˜ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. 

์ƒ์—…์ ์ธ ์˜๋„๊ฐ€ ์•„๋‹Œ ๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•ด๋†“๋Š” ๋ชฉ์ ์œผ๋กœ ํฌ์ŠคํŒ… ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

www.inflearn.com/course/javascript-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EA%B0%95%EC%A2%8C#

 

์›น๋ธŒ๋ผ์šฐ์ € Javascript (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ) - ์ธํ”„๋Ÿฐ

์ƒํ™œ์ฝ”๋”ฉ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ ์ž…๋‹ˆ๋‹ค. ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ง์ ‘ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ณต๋ถ€ํ•ด ๋ด…๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ ์›น ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด JavaScript ์˜จ๋ผ์ธ ๊ฐ•์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ

www.inflearn.com


jQuery ๊ฐ์ฒด๋ž€?

 

jQuery ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’์œผ๋กœ jQuery ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์„ ํƒํ•œ ์—˜๋ฆฌ๋จผํŠธ๋“ค์— ๋Œ€ํ•ด์„œ ์ฒ˜๋ฆฌํ•  ์ž‘์—…์„ ํ”„๋กœํผํ‹ฐ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋‹ค.

<script>
	var li = $('li');

/*
li  : jQuery Object
$~~ : jQuery function
*/
</script>

 

์œ„ ์ฝ”๋“œ์—์„œ ๋งŒ์•ฝ li.css('text-decoration','underline'); ๋ผ๊ณ  ํ•œ๋‹ค๋ฉด li์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ๊ฐ’์˜ CSS๊ฐ€ ๋ณ€๊ฒฝ ๋  ๊ฒƒ์ด๋‹ค.

์ด๋ ‡๊ฒŒ, ๋‚ด๋ถ€์ ์œผ๋กœ ํ•œ ์ฐจ๋ก€์”ฉ ๋ฐ˜๋ณต์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋ฐ”๋กœ "์•”์‹œ์  ๋ฐ˜๋ณต"์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

์•”์‹œ์  ๋ฐ˜๋ณต

jQuery ๊ฐ์ฒด์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํŠน์„ฑ์€ ์•”์‹œ์ ์ธ ๋ฐ˜๋ณต์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. DOM๊ณผ ๋‹ค๋ฅด๊ฒŒ jQuery ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์„ ํƒ๋œ ์—˜๋ฆฌ๋จผํŠธ ์ „์ฒด์— ๋Œ€ํ•ด์„œ ๋™์‹œ์— ์ž‘์—…์ด ์ฒ˜๋ฆฌ๋œ๋‹ค.

์•”์‹œ์  ๋ฐ˜๋ณต์€ ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ๋งŒ ๋™์ž‘ํ•œ๋‹ค. ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋Š” ์„ ํƒ๋œ ์—˜๋ฆฌ๋จผํŠธ ์ค‘ ์ฒซ๋ฒˆ์งธ์— ๋Œ€ํ•œ ๊ฐ’๋งŒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜์—์„œ ์‚ดํŽด๋ณธ๋‹ค.

 

 

jQuery์—์„œ๋Š” ๋ณดํ†ต, ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ชจ๋‘ ๋ช…์‹œํ•ด์ฃผ๋ฉด "์„ค์ •"์„ ์˜๋ฏธํ•˜๊ณ , ํ•œ ๊ฐ€์ง€์˜ ์ธ์ž๋งŒ ๋ช…์‹œํ•œ๋‹ค๋ฉด "๊ฐ€์ ธ์˜จ๋‹ค"๋ผ๋Š” ๋œป์„ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰ ์ธ์ž๋กœ ๋‘๊ฐ€์ง€๋ฅผ ๋„ฃ์ง€ ์•Š๊ณ  ํ•˜๋‚˜๋งŒ ์จ์ฃผ๋Š” ๊ฒฝ์šฐ์—” ๊ทธ๊ฒƒ์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋‚˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

 

์ฒด์ด๋‹

chainig์ด๋ž€ ์„ ํƒ๋œ ์—˜๋ฆฌ๋จผํŠธ์— ๋Œ€ํ•ด์„œ ์—ฐ์†์ ์œผ๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ผ์„ ์—ฐ์†์ ์œผ๋กœ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Œ.

 

์กฐํšŒ ๊ฒฐ๊ณผ

jQuery ๊ฐ์ฒด์—๋Š” ์กฐํšŒ๋œ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค. jQuery ๊ฐ์ฒด๋Š” ์ผ์ข…์˜ ์œ ์‚ฌ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ์กฐํšŒ๋œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด์„œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

<ul>
    <li>html</li>
    <li>css</li>
    <li>JavaScript</li>
</ul>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
    console.log($('li').length);
    console.log($('li')[0]);
    var li = $('li');
    for(var i=0; i<li.length; i++){
        console.log(li[i]);
    }
</script>

 

ํ•œ๊ฐ€์ง€ ์ฃผ์˜ํ•  ๊ฒƒ์€ li[i]์˜ ๊ฐ’์€ ํ•ด๋‹น ์—˜๋ฆฌ๋จผํŠธ์— ๋Œ€ํ•œ jQuery ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ DOM ๊ฐ์ฒด๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

๋”ฐ๋ผ์„œ jQuery์˜ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด์„œ ์ด ๊ฐ์ฒด๋ฅผ ์ œ์–ดํ•˜๋ ค๋ฉด jQuery ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.

<script>
    for(var i=0; i<li.length; i++){
        $(li[i]).css('color', 'red');
    }
</script>

 

์ข€๋” 'jQuery'๋‹ค์šด ์กฐํšŒ ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ?

 

 

์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ .map()์„ ์ด์šฉํ•˜์—ฌ ์กฐํšŒ๋œ ๊ฒฐ๊ณผ๋ฅผ ์—ด๋žŒํ• ์ˆ˜๋„ ์žˆ๋‹ค.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<ul>
		<li>html</li>
		<li>css</li>
		<li>Javascript</li>
</ul>

	
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script>
	var li = $('li');
	li.map(function(index, elem){
		console.log(index, elem);
		$(elem).css('color','red');

	})

</script>
</body>
</html>

map์€ jQuery ๊ฐ์ฒด์˜ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•œ๋‹ค. ์ด ๋•Œ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋Š”๋ฐ

์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ์—˜๋ฆฌ๋จผํŠธ์˜ ์ธ๋ฑ์Šค, ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ์—˜๋ฆฌ๋จผํŠธ ๊ฐ์ฒด(DOM)์ด ์ „๋‹ฌ๋œ๋‹ค. ์ฐธ๊ณ 

 

.map() | jQuery API Documentation

Description: Pass each element in the current matched set through a function, producing a new jQuery object containing the return values. If you wish to process a plain array or object, use the jQuery.map() instead. As the return value is a jQuery object,

api.jquery.com

 

jQuery ๊ฐ์ฒด API

์ œ์–ดํ•  ๋Œ€์ƒ์„ ์„ ํƒํ•œ ํ›„์—๋Š” ๋Œ€์ƒ์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ํ•ด์•ผํ•œ๋‹ค. .css์™€ .attr์€ jQuery ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”์†Œ๋“œ ์ค‘์˜ ํ•˜๋‚˜์ธ๋ฐ, jQuery๋Š” ๊ทธ ์™ธ์—๋„ ๋งŽ์€ API๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ jQuery API๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

 

https://api.jquery.com

 

jQuery API Documentation

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. If you're new t

api.jquery.com

 

์•„๋ž˜๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ž์ฃผ์“ฐ๋Š” .css๋ฉ”์†Œ๋“œ์˜ ๋Œ€ํ•œ ์‚ฌ์šฉ๋ฒ•๊ณผ ์ •์˜์ด๋‹ค.

api.jquery.com/css/

 

.css() | jQuery API Documentation

Description: Set one or more CSS properties for the set of matched elements. As with the .prop() method, the .css() method makes setting properties of elements quick and easy. This method can take either a property name and value as separate parameters, or

api.jquery.com

 

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.