[Js] jQuery ๊ฐ์ฒด | map() | jQuery API
- -
๋ชจ๋ ํฌ์คํ ์ ์ธํ๋ฐ [์ํ์ฝ๋ฉ] ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์์ ๋ด์ฉ์ ๋ด๊ณ ์์ผ๋ฉฐ
์ถ์ฒ๋ ์๋์ ์ฃผ์๋ก "egoing Lee"๋์ ์๋ฃ์ ๋๋ค.
์์ ์ ์ธ ์๋๊ฐ ์๋ ๊ณต๋ถํ ๊ฒ์ ์ ๋ฆฌํด๋๋ ๋ชฉ์ ์ผ๋ก ํฌ์คํ ํ ๊ฒ์ ๋๋ค.
์น๋ธ๋ผ์ฐ์ 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๋ฅผ ์ฐธ๊ณ ํ์.
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๋ฉ์๋์ ๋ํ ์ฌ์ฉ๋ฒ๊ณผ ์ ์์ด๋ค.
.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
'Languages > Java Script' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Js] ์๋ณ์ API | ์กฐํ API | ์์ฑ API (0) | 2021.02.10 |
---|---|
[Js] Element ๊ฐ์ฒด | Node | ์๋ณ์ | ์กฐํ | ์๋ฆฌ๋จผํธ (0) | 2021.02.08 |
[Js] HTML Collection | DOM | javascript (0) | 2021.02.08 |
[Js] HTML Element | ๋จ์์ ๋ณต์ | DOM (0) | 2021.02.08 |
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค