Mainpulation(์—˜๋ฆฌ๋จผํŠธ ์ œ์–ด)

2023. 7. 28. 13:55ใ†jquery

jQuery ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ œ์–ดํ•˜๋Š” ์ผ๊ด€๋˜๊ณ  ํ’๋ถ€ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณต

 

- ์ž์‹์œผ๋กœ ์‚ฝ์ž…

.append(), .appendTo(), .html(), .prepend(), .prependTo(), .text()

<!DOCTYPE html>
<html>
    <head>
        <style>
            p {
                background:yellow;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
    	<!-- pํƒœ๊ทธ : ํ•˜๋‚˜์˜ ๋ฌธ๋‹จ -->
        <p>
            I would like to say:
        </p>
    
        <script>$("p").append("<strong>Hello</strong>");</script>
        
    </body>
</html>

 

- ํ˜•์ œ๋กœ ์‚ฝ์ž…

.after(), .before(), .insertAfter(), .insertBefore()

<!DOCTYPE html>
<html>
    <head>
        <style>
            p {
                background:yellow;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <p>
            I would like to say:
        </p>
        <script>$("p").after("<b>Hello</b>");</script>
    </body>
</html>

.after() ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ํ˜•์ œ๋กœ ์‚ฝ์ž…!

 

- ์„ ํƒํ•œ ํƒœ๊ทธ ๋ถ€๋ชจ๋กœ ๊ฐ์‹ธ๊ธฐ

.unwrap(), .wrap(), .wrapAll(), .wrapInner()

<!DOCTYPE html>
<html>
    <head>
        <style>
            div {
                border:2px blue solid;
                margin:2px;
                padding:2px;
            }
            p {
                background:yellow;
                margin:2px;
                padding:2px;
            }
            strong {
                color:red;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <span>Span Text</span>
        <strong>What about me?</strong>
        <span>Another One</span>
        <script>$("span").wrap("<div><div><p><em><b></b></em></p></div></div>");</script>
    </body>
</html>

.wrap() ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜์˜ ์—ฌ๋Ÿฌ๋ถ€๋ชจํƒœ๊ทธ๋กœ ๊ฐ์‹ธ๋Š” ๊ฒƒ <b> </b> ์‚ฌ์ด์— <span> ๋“ค์–ด๊ฐ!

 

 

- ์‚ญ์ œ

.detach(), .empty(), .remove(), .unwrap()

<!DOCTYPE html>
<html>
    <head>
        <style>
            p {
                background:yellow;
                margin:6px 0;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <p>
            Hello
        </p>
        how are
        <p>
            you?
        </p>
        <button>
            Call remove() on paragraphs
        </button>
        <script>
            $("button").click( function () {
                $("p").remove();
            });
        </script>
    </body>
</html>

๋ฒ„ํŠผ ํด๋ฆญ ์‹œ 

์•„๋ž˜์ฒ˜๋Ÿผ p ํƒœ๊ทธ ์‚ญ์ œ >  how are๊ณผ ๋ฒ„ํŠผ ํƒœ๊ทธ๋งŒ ๋‚จ์Œ!

 

- ์น˜ํ™˜

.replaceAll(), .replaceWith()

 

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <p> Hello </p>
        <p> cruel </p>
        <p> World </p>
        <script>$("<b>Paragraph. </b>").replaceAll("p"); </script>
    </body>
</html>

.replaceAll() ์‚ฌ์šฉ ์‹œ element ์ƒ์„ฑ ๋œ๋‹ค๊ณ  ๋ณด๋ฉด ๋จ!

๋ฌธ๋ฒ• :

replaceAll(target-selector)

 

์ธ์ž๋กœ target์ด ๋“ค์–ด์˜ค๊ณ  ๊ทธ  target๋Š” ์„ ํƒ์ž์ž„!

pํƒœ๊ทธ๋ฅผ bํƒœ๊ทธ๋กœ ๋‹ค ์น˜ํ™˜.

*.replaceWith() ๋ฌธ๋ฒ• :

.replaceWith( newContent )

* ์ฃผ์˜! h1์˜ ๋‚ด์šฉ ๋ฟ ์•„๋‹ˆ๋ผ h1 ํƒœ๊ทธ๊นŒ์ง€ ์ง€์šฐ๊ณ  ๋ฐ”๊ฟˆ

 

ex)

์ถœ์ฒ˜ : https://www.codingfactory.net/10344

  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'button' ).click( function() {
          $( 'h1' ).replaceWith( '<p>Ipsum</p>' );
        } );
      } );
    </script>
  </head>
  <body>
    <h1>Lorem</h1>
    <button>Replace</button>
  </body>

๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด h1 ์š”์†Œ๋ฅผ <p>Ipsum</p>๋กœ ์น˜ํ™˜

 

 

- ํด๋ž˜์Šค ์ œ์–ด

.addClass(), .hasClass(), .removeClass(), .toggleClass()

<!DOCTYPE html>
<html>
    <head>
        <style>p {
                margin: 4px;
                font-size:16px;
                font-weight:bolder;
                cursor:pointer;
            }
            .blue {
                color:blue;
            }
            .highlight {
                background:yellow;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <p class="blue"> Click to toggle </p>
        <p class="blue highlight"> highlight </p>
        <p class="blue"> on these </p>
        <p class="blue"> paragraphs </p>
        <script>
             $("p").click( function () {
                 $(this).toggleClass("highlight");
             });
         </script>
    </body>
</html>

.toggleClass()๋กœ ์„ ํƒํ•œ ์š”์†Œ์— ํด๋ž˜์Šค ๊ฐ’์„ ๋„ฃ์—ˆ๋‹ค ๋บ๋‹ค ํ•  ์ˆ˜ ์žˆ์Œ.

ํด๋ฆญ ์‹œ class "highlight" ๋น ์ง

 

- ์†์„ฑ ์ œ์–ด

.attr(), .prop(), .removeAttr(), .removeProp(), .val()

<html>
    <head>
        <style>p {
                color:blue;
                margin:8px;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
    	<script type="text/javascript">
       		
			//ํƒ€์ดํ•‘์„ ๋—์„ ๋•Œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ
		$("input").keyup( function () {
                var value = $(this).val();
		//pํƒœ๊ทธ์— value ๊ฐ’์„ text๋ผ๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ(์—˜๋ฆฌ๋จผํŠธ์˜ content ์˜์—ญ์— ์ „๋‹ฌ๋œ ์ธ์ž ์…‹ํŒ…ํ•˜๋Š” ๊ฒƒ)
                $("p").text(value);
            }).keyup();
            //ํŠธ๋ฆฌ๊ฑฐ > keyup์ด๋ผ๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ฐ€์ง„ ํ•ธ๋“ค๋Ÿฌ ํ˜ธ์ถœ > input ํƒœ๊ทธ ๋ฐ‘์— ์ž…๋ ฅํ•œ ๊ธ€์ž ๋‚˜์˜ค๊ฒŒ ๋จ
    	</script>
    </body>
</html>

val()์„ ์‚ฌ์šฉํ•˜์—ฌ ํผ ์œ ํ˜•์˜ ํƒœ๊ทธ์˜ value ๊ฐ’ ์ œ์–ด

* .attr(), .prop()์˜ ์ฐจ์ด

.attr()์€ ์†์„ฑ ๊ทธ ์ž์ฒด์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜

.attr()๋Š” ์š”์†Œ์˜ ์†์„ฑ๊ฐ’์ด๋‚˜ ์ •๋ณด๋ฅผ ์กฐํšŒ(style, src, rowspan ๋“ฑ)ํ•˜๊ฑฐ๋‚˜ ์„ธํŒ…ํ•˜๋Š” ํ˜•์‹

.prop()์€ ์†์„ฑ๊ฐ’์˜ ์ƒํƒœ๋ฅผ ํŒ๋‹จ

.prop()๋Š” ์š”์†Œ๊ฐ€ ๊ฐ€์ง„ ์‹ค์ œ์ ์ธ ์ƒํƒœ(ํ™œ์„ฑํ™”, ์ฒดํฌ, ์„ ํƒ์—ฌ๋ถ€ ๋“ฑ)๋ฅผ ์ œ์–ด

<input type="checkbox" id="check1" checked>check1

์ฒดํฌ ๋ฐ•์Šค ํด๋ฆญ ์‹œ ์ฒซ๋ฒˆ์งธ alert์€ attr ์†์„ฑ , ๋‘๋ฒˆ์งธ  alert์€ prop ์†์„ฑ์ด ๋œจ๊ฒŒ ์„ค์ •

//# attr(), prop()์˜ ์ฐจ์ด
$("input").click(function(){
    alert($('#check1').attr("checked"));
    alert($('#check1').prop("checked"));
});

attr :

check1์˜ ์†์„ฑ๊ฐ’ ๊ทธ๋Œ€๋กœ๋ฅผ ๊ฐ€์ ธ์˜ด

 

prop :

check1์˜ checked ์†์„ฑ์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€ ํŒ๋‹จ

 

'jquery' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

traversing(ํƒ์ƒ‰)  (0) 2023.07.28
Form  (0) 2023.07.28
event  (0) 2023.07.28
Chain  (0) 2023.07.27
์„ ํƒ์ž  (0) 2023.07.27