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 ์์ฑ์ด ์๋์ง ์ฌ๋ถ ํ๋จ