์ƒˆ์†Œ์‹

Languages/Java Script

[Js] jQuery ์†์„ฑ ์ œ์–ด API | jQuery ์กฐํšŒ ๋ฒ”์œ„ ์ œํ•œ | javascript

  • -
๋ฐ˜์‘ํ˜•

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

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "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๋ฅผ ์†์„ฑ ์ œ์–ด API์™€ ๋‹ค์Œ ๊ฐ•์ขŒ์ธ jQuery ์กฐํšŒ ๋ฒ”์œ„ ์ œํ•œ ๋‚ด์šฉ์„ ๋ชจ๋‘ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.



jQuery ์†์„ฑ ์ œ์–ด API

 

์†์„ฑ์ œ์–ด

jQuery ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ ์ค‘ setAttribute, getAttribute์— ๋Œ€์‘๋˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” attr์ด๋‹ค. ๋˜ํ•œ removeAttribute์— ๋Œ€์‘๋˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ๋Š” removeAttr์ด ์žˆ๋‹ค.

<!DOCTYPE html>
<html>
<head>
	<title> jQuery ์†์„ฑ์˜ ์ œ์–ด</title>
</head>
<body>

<a id="target" href="http://opentutorials.org">opentutorials</a>
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>

<script>
    var t = $('#target');
    console.log(t.attr('href')); //http://opentutorials.org
    t.attr('title', 'opentutorials.org'); // title ์†์„ฑ์˜ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.
    t.removeAttr('title'); // title ์†์„ฑ์„ ์ œ๊ฑฐํ•œ๋‹ค.
</script>
</body>
</html>

 

attribute์™€ property

DOM๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ jQuery๋„ ์†์„ฑ(attribute)๊ณผ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค. ์†์„ฑ์€ attr, ํ”„๋กœํผํ‹ฐ๋Š” prop ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

<!DOCTYPE html>
<html>
<head>
	<title> jQuery ์†์„ฑ์˜ ์ œ์–ด</title>
</head>
<body>

<a id="t1" href="./demo.html">opentutorials</a>

<input id="t2" type="checkbox" checked="checked" />

<script src="https://code.jquery.com/jquery-2.2.4.min.js"
 integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
 crossorigin="anonymous"></script>

<script>
// ํ˜„์žฌ ๋ฌธ์„œ์˜ URL์ด ์•„๋ž˜์™€ ๊ฐ™๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ
// http://localhost/jQuery_attribute_api/demo2.html
	var t1 = $('#t1');
	console.log(t1.attr('href')); // ./demo.html 
	console.log(t1.prop('href')); // file:///C:/Users/User/Desktop/javascript/demo.html
 
	var t2 = $('#t2');
	console.log(t2.attr('checked')); // checked
	console.log(t2.prop('checked')); // true
</script>


</body>
</html>

 

์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ฝ˜์†”์ฐฝ์—์„œ ํ™•์ธํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

jQuery๋ฅผ ์ด์šฉํ•˜๋ฉด ํ”„๋กœํผํ‹ฐ์˜ ์ด๋ฆ„์œผ๋กœ ์–ด๋–ค ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๊ฑด ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์œผ๋กœ ๊ต์ •ํ•ด์ค€๋‹ค.

์ด๋Ÿฐ ๊ฒƒ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์˜๋ผ๊ณ  ํ• ์ˆ˜ ์žˆ๊ฒ ๋‹ค.

<!DOCTYPE html>
<html>
<head>
	<title> jQuery ์†์„ฑ์˜ ์ œ์–ด</title>
</head>
<body>

<div id="t1">opentutorials</div>
<div id="t2">opentutorials</div>

<script src="https://code.jquery.com/jquery-2.2.4.min.js"
 integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
 crossorigin="anonymous"></script>
<script>
$('#t1').prop('className', 'test1'); 
$('#t2').prop('class', 'test2');  
</script>


</body>
</html>

 

๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด, jQuery๋‚ด๋ถ€์—์„œ ์•Œ์•„์„œ ๊ต์ •์„ ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๊ฐ’ ๋ชจ๋‘ ์ •์ƒ์ ์œผ๋กœ ์ ์šฉ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

(์ฐธ๊ณ ๋กœ : attr -> ์†์„ฑ๋ฐฉ์‹, prop -> ํ”„๋กœํผํ‹ฐ ๋ฐฉ์‹์ž„)

 

 

jQuery ์กฐํšŒ ๋ฒ”์œ„ ์ œํ•œ

selector context

๊ฐ€์žฅ ๊ฐ„ํŽธํ•œ ๋ฐฉ๋ฒ•์€ ์กฐํšŒํ•  ๋•Œ ์กฐํšŒ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ์ œํ•œ๋œ ๋ฒ”์œ„๋ฅผ jQuery์—์„œ๋Š” selector context๋ผ๊ณ  ํ•œ๋‹ค.

<!DOCTYPE html>
<html>
<head>
	<title> jQuery ์กฐํšŒ ๋ฒ”์œ„ ์ œํ•œ</title>
</head>
<body>

<ul>
    <li class="marked">html</li>
    <li>css</li>
    <li id="active">JavaScript
        <ul>
            <li>JavaScript Core</li>
            <li class="marked">DOM</li>
            <li class="marked">BOM</li>
        </ul>
    </li>
</ul>

<script src="https://code.jquery.com/jquery-2.2.4.min.js"
 integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
 crossorigin="anonymous"></script>
<script>
    $( ".marked", "#active").css( "background-color", "red" );
</script>
</body>
</html>

์—ฌ๊ธฐ์—์„œ .marked๋Š” ํšจ๊ณผ๋ฅผ ์ฃผ๊ณ ์ž ํ•˜๋Š” ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ง€์ •ํ•˜๊ณ , ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ #active ์ฆ‰ selector context๊ฐ€ ์˜ค๋Š”๋ฐ, ์ด๋Š” ์ฒซ๋ฒˆ์งธ ์ธ์ž๋ฅผ ์ œ์–ดํ•˜๋ ค๊ณ  ํ•˜๋Š” ์—˜๋ฆฌ๋จผํŠธ ์ด๋‹ค.

 

์‹คํ–‰๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๊ฐ€ ์ ์šฉ๋œ๋‹ค.

<ul>
    <li class="marked">html</li>
    <li>css</li>
    <li id="active">JavaScript
        <ul>
            <li>JavaScript Core</li>
            <li class="marked" style="background-color: red;">DOM</li>
            <li class="marked" style="background-color: red;">BOM</li>
        </ul>
    </li>
</ul>

 

์กฐ๊ธˆ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, ์„ ํƒ์ž๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ด๋„ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™๋‹ค.

$( "#active .marked").css( "background-color", "red" );

 

.find()

 

find๋Š” jQuery ๊ฐ์ฒด ๋‚ด์—์„œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์œ„์˜ ์˜ˆ์ œ์™€ ํšจ๊ณผ๊ฐ€ ๊ฐ™๋‹ค.

$( "#active").find('.marked').css( "background-color", "red" );

 

find๋ฅผ ์“ฐ๋Š” ์ด์œ ๋Š” ์ฒด์ธ์„ ๋Š์ง€ ์•Š๊ณ  ์ž‘์—…์˜ ๋Œ€์ƒ์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

๊ธฐ๋ณธ ์˜ˆ์ œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•ด๋ณด์ž.

$('#active').css('color','red').find('.marked').css('background-color','yellow')

 

 

์‹คํ–‰๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<ul>
    <li class="marked">html</li>
    <li>css</li>
    <li id="active" style="color: red;">JavaScript
        <ul>
            <li>JavaScript Core</li>
            <li class="marked" style="background-color: yellow;">DOM</li>
            <li class="marked" style="background-color: yellow;">BOM</li>
        </ul>
    </li>
</ul>

 

 

์ „์ฒด์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<!DOCTYPE html>
<html>
<head>
	<title> jQuery ์กฐํšŒ ๋ฒ”์œ„ ์ œํ•œ</title>
</head>
<body>

<ul>
    <li class="marked">html</li>
    <li>css</li>
    <li id="active">JavaScript
        <ul>
            <li>JavaScript Core</li>
            <li class="marked">DOM</li>
            <li class="marked">BOM</li>
        </ul>
    </li>
</ul>

<script src="https://code.jquery.com/jquery-2.2.4.min.js"
 integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
 crossorigin="anonymous"></script>
<script>
    //$( ".marked", "#active").css( "background-color", "red" );
    $('#active').css('color','red').find('.marked').css('background-color','yellow')
</script>
</body>
</html>

 

์ฆ‰ li.item-li ์—˜๋ฆฌ๋จผํŠธ์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์—˜๋ฆฌ๋จผํŠธ์˜ ์ „๊ฒฝ์ƒ‰์„ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝํ•œ ํ›„์— li ์—˜๋ฆฌ๋จผํŠธ๋งŒ์„ ์กฐํšŒํ•ด์„œ ๋ฐฐ๊ฒฝ์ƒ‰์„ ๋…ธ๋ž€์ƒ‰์œผ๋กœ ์ง€์ •ํ•˜๊ณ  ์žˆ๋‹ค.

find๋ฅผ ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋œ๋‹ค. 

๋” ๋งŽ์€ ์˜ˆ์ œ๋Š” jQuery์˜ ๋ฉ”๋‰ด์–ผ์„ ์ฐธ๊ณ ํ•˜์ž. .find()

 

.find() | jQuery API Documentation

Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. Given a jQuery object that represents a set of DOM elements, the .find() method allows us to search through the desc

api.jquery.com

 

๋ฐ˜์‘ํ˜•
Contents

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

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