-
๊ธฐ๋ณธ์ ํ์ ํ ๋์ ๋ณด๊ธฐ โผ
-
์ธ์ ๊ด๊ณ ์ ํ์ ์ ๋ฆฌ
-
1. ๋ถ๋ชจ์์ ์ ํ์
-
2. ํ์์์ ์ ํ์
-
3. ์์์์ ์ ํ์
-
4. ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์
-
5. ์ ์ฒด ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์
-
6. ์ ์ฒด ํ์ ์์ ์ ํ์
-
7. ๋ฒ์ ์ ํ ์ ์ฒด ํ/๋์ ์์ ์ ํ์
-
8. ์์ ์์ ์ ํ์
-
9. ๊ฐ์ฅ ๊ฐ๊น์ด ์์ ์์ ์ ํ์

ํจ๊ป ๋ณด๋ฉด ์ข์ ๊ด๋ จ ํฌ์คํ โผ
[JavaScript] jQuery๋ฅผ ์ฌ์ฉํ ๋ ์ ์ํด์ผ ํ ์ฌํญ๋ค
โญ ๋ฌธ์๊ฐ ๋ก๋๋ ํ ์คํํ์โญ jQuery ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋จผ์ ๋ก๋ํ์โญ $ ์ถฉ๋ ๋ฐฉ์ง ํ์ ์ noConflict โญ ์ด๋ฒคํธ ์ค๋ณต ๋ฐฉ์ง off()โญ AJAX ์บ์ฑ ๋ฐฉ์ง (cache: false) โญ ๋์ ์์์ ์ด๋ฒคํธ ์ ์ฉ on()โญ ์ฑ๋ฅ ์ต
awesomepossum.tistory.com
[JavaScript] Object(๊ฐ์ฒด), DOM(๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ), function() ํจ์ ๊ธฐ์ด ์ค์ต
1. Object (๊ฐ์ฒด) 2. DOM (Document Object Model)2-1. DOM(๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ) ๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ(DOM) HTML CSS JS 2-2. innerHTML vs textContent ์ค๋์ ๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ์ ๋ํด์ ๋ฐฐ์๋ณด์๋ค 2-3. querySelector vs getElementByID์์ฆ์๋
awesomepossum.tistory.com
์ค๋์ ์ ์ด์ฟผ๋ฆฌ ์ ํ์๋ก ์คํ์ผ์ด๋ ์์ฑ์ ์ ์ฉํ๋ ๋ฒ์ ์ ๋ฆฌํด๋ณธ๋ค.
์ ํ์๋ CSS๋ฅผ ์ ๋ค๋ฃจ๊ธฐ ์ํด ๊ผญ ์์์ผ ํ ์ค์ํ ๋ถ๋ถ์ด๋ค.
์ ์ด์ฟผ๋ฆฌ์์๋ $() ์๊ดํธ ์์ ๋ฌธ์ํ ๋ฐ์ดํฐ " "๋ก CSS ์ ํ์๋ฅผ ์ ๋ ฅํ๋ฉด ๋๋ค.
์ด ๋, .css() ๋ฅผ ์ฌ์ฉํ๋ฉด ์คํ์ผ์ ๋ณ๊ฒฝํ ์ ์๊ณ , .attr() ๋๋ .prop()์ ์ฌ์ฉํ๋ฉด HTML ์์ฑ์ ์กฐ์ํ ์ ์๋ค.
1. ์ ํํ ์์์ ์ง์ ํ ์คํ์ผ ์ ์ฉํ๊ธฐ
$(์์์ ํ).css(์์ฑ, ๊ฐ)
// ํน์ ์์์ ์คํ์ผ ์ ์ฉ
$(".my-class").css("color", "red"); // ๊ธ์์์ ๋นจ๊ฐ์์ผ๋ก ๋ณ๊ฒฝ
$("#my-id").css({
"background-color": "yellow",
"font-size": "20px"
}); // ๋ฐฐ๊ฒฝ์๊ณผ ๊ธ์ ํฌ๊ธฐ ๋ณ๊ฒฝ
2. ์ ํํ ์์์ ์ง์ ํ ์์ฑ ์ ์ฉํ๊ธฐ
$(์์์ ํ).attr(์์ฑ, ๊ฐ)
// ํน์ ์์์ ์์ฑ ์ถ๊ฐ ๋๋ ๋ณ๊ฒฝ
$("input").attr("placeholder", "๊ฐ์ ์
๋ ฅํ์ธ์"); // placeholder ์ถ๊ฐ
$(".my-link").attr("href", "https://example.com"); // ๋งํฌ ๋ณ๊ฒฝ
$("#my-checkbox").prop("checked", true); // ์ฒดํฌ๋ฐ์ค ์ ํ ์ํ๋ก ๋ณ๊ฒฝ
๋ชจ๋ ์์๋ค์ํผ CSS ์คํ์ผ์ CSS ํ์ผ์ ์ ์ํด์ HTML ํ์ผ๊ณผ ๋ฐ๋ก ๊ด๋ฆฌํ๋ ๊ฒ์ด ๊ฐ์ฅ ๊ถ์ฅ๋๋ ๋ฐฉ๋ฒ์ด๋ค.
jQuery์์๋ ๋์ ์ผ๋ก ์คํ์ผ์ ๋ณ๊ฒฝํด์ผ ํ ๋๋ง .css()๋ฅผ ์ฌ์ฉํ์.
๋ค๋ง ์์ฑ์ ๊ฒฝ์ฐ, CSS๊ฐ ์๋๋ผ HTML ์์์ ์์ฑ์ด๊ธฐ ๋๋ฌธ์ jQuery์ attr() ๋๋ prop()์ ์ฌ์ฉํด์ ๋ณ๊ฒฝํ๋ ๊ฒ ์์ฐ์ค๋ฝ๋ค.
๊ธฐ๋ณธ์ ํ์ ํ ๋์ ๋ณด๊ธฐ โผ
๊ตฌ๋ถ | ์ข ๋ฅ | ์ฌ์ฉ๋ฒ | ์ค๋ช |
์ง์ ์ ํ์ |
์ ์ฒด ์ ํ์ | $("*") | ๋ชจ๋ ์์ ์ ํํ๊ธฐ |
์์ด๋ ์ ํ์ | $("#์์ด๋๋ช ") | id ์์ฑ์ ์ง์ ํ ๊ฐ ๊ฐ์ง ์์ ์ ํํ๊ธฐ | |
ํด๋์ค ์ ํ์ | $(".ํด๋์ค๋ช ") | class ์์ฑ์ ์ง์ ํ ๊ฐ ๊ฐ์ง ์์ ์ ํํ๊ธฐ | |
์์ ์ ํ์ | $("์์๋ช ") | ์ง์ ํ ์์๋ช ๊ณผ ์ผ์นํ๋ ์์๋ค๋ง ์ ํํ๊ธฐ | |
๊ทธ๋ฃน ์ ํ์ | $("์ ํ1, ์ ํ2, ์ ํ3,... ์ ํn") | ์ ํ1, ... ์ ํn์ ์ง์ ๋ ์์๋ค์ ํ๋ฒ์ ์ ํํ๊ธฐ | |
์ข ์ ์ ํ์ | $("p.txt_1") $("p#txt_1") |
<p> ์์ ์ค class ๊ฐ์ด txt_1์ธ ์์ ๋๋ id๊ฐ์ด txt_1 ์ธ ์์ ์ ํํ๊ธฐ | |
์ธ์ ๊ด๊ณ ์ ํ์ |
๋ถ๋ชจ ์์ ์ ํ์ | $("์์ ์ ํ").parent() | ์ ํํ ์์์ ๋ถ๋ชจ ์์ ์ ํํ๊ธฐ |
์์ ์์ ์ ํ์ | $("์์ ์ ํ").parents() | ์ ํํ ์์์ ์์ ์์๋ฅผ ๋ชจ๋ ์ ํํ๊ธฐ | |
๊ฐ์ฅ ๊ฐ๊น์ด ์์ ์์ ์ ํ์ | $("์์ ์ ํ").closest("div") | ์ ํํ ์์์ ์์ ์์ ์ค ๊ฐ์ฅ ๊ฐ๊น์ด <div>๋ง ์ ํํ๊ธฐ | |
ํ์ ์์ ์ ํ์ | $("๊ธฐ์ค ์์1 ํ์ ์์2") | ์ ํํ ์์์ ์ง์ ํ ํ์ ์์๋ฅผ ์ ํํ๊ธฐ | |
์์ ์์ ์ ํ์ | $("์์ ์ ํ>์์์์") | ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ๊ด๊ณ์ ์ง์ ํ ์์๋ง ์ ํํ๊ธฐ | |
์์ ์์๋ค ์ ํ์ | $("์์ ์ ํ").children() | ์ ํํ ์์์ ๋ชจ๋ ์์ ์์๋ฅผ ์ ํํ๊ธฐ | |
ํ(์ด์ ) ์์ ์ ํ์ | $("์์ ์ ํ").prev() | ์ ํํ ์์์ ๋ฐ๋ก ์ด์ ์์๋ฅผ ์ ํํ๊ธฐ | |
ํ(์ด์ ) ์์๋ค ์ ํ์ | $("์์ ์ ํ").prevAll() | ์ ํํ ์์์ ์ด์ ์์ ๋ชจ๋๋ฅผ ์ ํํ๊ธฐ | |
์ง์ ํ(์ด์ ) ์์๋ค ์ ํ์ |
$("์์ ์ ํ").prevUntil("์์๋ช ") | ์ ํํ ์์๋ถํฐ ์ง์ ํ ์์์ ์ด์ ์์๊น์ง ๋ชจ๋ ์ ํํ | |
๋์(๋ค์) ์์ ์ ํ์ | $("์์ ์ ํ").next() $("์์ ์ ํ+๋ค์ ์์") |
์ ํํ ์์์ ๋ค์ ์์๋ฅผ ์ ํํ๊ธฐ | |
๋์(๋ค์) ์์๋ค ์ ํ์ | $("์์ ์ ํ").nextAll() | ์ ํํ ์์์ ๋ค์ ์์๋ฅผ ๋ชจ๋ ์ ํํ๊ธฐ | |
์ง์ ๋์(๋ค์) ์์๋ค ์ ํ์ |
$("์์ ์ ํ").nextUntil("h2") | ์ ํํ ์์๋ถํฐ ์ง์ ํ ์์์ ๋ค์ ์์๊น์ง ๋ชจ๋ ์ ํํ๊ธฐ | |
์ ์ฒด ํ์ ์์ ์ ํ์ | $(".box_1").siblings() | class ๊ฐ์ด box_1์ธ ์์์ ํ์ ์์ ์ ์ฒด๋ฅผ ์ ํํ๊ธฐ |
์ธ์ ๊ด๊ณ ์ ํ์ ์ ๋ฆฌ
1. ๋ถ๋ชจ์์ ์ ํ์
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<script>
$(function(){
$("#list_1").parent()
.css("border", "2px dashed #f00")
.css("background", "yellow")
});
</script>
<h1>๋ถ๋ชจ์์ ์ ํ์</h1>
<ul id="wrap">
<li>๋ฆฌ์คํธ1
<ul>
<li id="list_1">๋ฆฌ์คํธ1-1</li> <!-- <ul>์ ์์์ธ <li>ํ๊ทธ -->
<li>๋ฆฌ์คํธ1-2</li>
</ul>
</li>
<li>๋ฆฌ์คํธ2</li>
<li>๋ฆฌ์คํธ3</li>
</ul>
</body>
</html>

2. ํ์์์ ์ ํ์
$("๊ธฐ์ค ์์ ์ ํ1 ์์ ์ ํ2")
$("#wrap h1") // id="warp"์ธ ์์๊ฐ ๊ธฐ์ค ์์
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<script>
$(function(){
$("#wrap h1").css({
"background-color":"yellow",
"border":"2px dashed #f00"
})
});
</script>
<div id="wrap">
<h1>์ธ์ ๊ด๊ณ ์ ํ์</h1>
<p>๋ด์ฉ 1</p>
<section>
<h1>ํ์ ์์ ์ ํ์</h1>
<p>๋ด์ฉ2</p>
</section>
</div>
</body>
</html>

3. ์์์์ ์ ํ์
$("์์ ์ ํ > ์์์์ ์ ํ")
$("์์ ์ ํ).children("์์ ์์ ์ ํ)
$("์์ ์ ํ).children()
์ ๋ ์ค์ ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ง์ ํ ์์ ์์๋ง ์ ํํ๋ค.
๋งจ ์๋ ์ฝ๋๋ ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ชจ๋ ์์ ์์๋ฅผ ์ ํํ๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<script>
$(function(){
$("#wrap > h1").css("border", "2px dashed #f00");
$("#wrap > section").children()
.css({
"background-color":"yellow",
"border":"2px solid #f00"
});
});
</script>
<div id="wrap">
<h1>์ธ์ ๊ด๊ณ ์ ํ์</h1>
<p>๋ด์ฉ 1</p>
<section>
<h1>์์ ์์ ์ ํ์</h1>
<p>๋ด์ฉ2</p>
</section>
</div>
</body>
</html>

4. ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์
ํ ์์ ์ ํ์๋ ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ก ์ด์ ํ์ ์์๋ง ์ ํํ๋ค.
๊ทธ๋ฆฌ๊ณ ๋์ ์์ ์ ํ์๋ ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ก ๋ค์ ํ์ ์์๋ง ์ ํํ๋ค.
์ ํํ ์์๋ฅผ (์์ ์ ํ1) ๊ธฐ์ค์ผ๋ก ๋ฐ๋ก ๋ค์์ ์ค๋ ์ ํํ ์์(์์ ์ ํ2)๋ง ์ ํํ๋ค.
$("์์ ์ ํ").prev()
$("์์ ์ ํ").next()
$("์์ ์ ํ1 + ์์ ์ ํ2")
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ ํ์ </title>
<script src="js/jquery.js"></script>
<script>
$(function(){
var style_1 = {
"background-color":"#0ff",
"border":"2px solid #f00"
}
var style_2 = {
"background-color":"#ff0",
"border":"2px dashed #f00"
}
$(".txt").prev()
.css(style_1);
$(".txt + p").css(style_2);
$(".txt").next().next()
.css(style_2);
});
</script>
</head>
<body>
<div id="wrap">
<h1>ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์</h1>
<p>๋ด์ฉ1</p>
<p class="txt">๋ด์ฉ2</p>
<p>๋ด์ฉ3</p>
<p>๋ด์ฉ4</p>
</div>
</body>
</html>

5. ์ ์ฒด ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์
$("์์ ์ ํ).prevAll()
$("์์ ์ ํ).nextAll()
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ ํ์ </title>
<script src="js/jquery.js"></script>
<script>
$(function(){
var style_1 = {
"background-color":"#0ff",
"border":"2px solid #f00"
}
var style_2 = {
"background-color":"#ff0",
"border":"2px dashed #f00"
}
$(".txt").prevAll( )
.css(style_1);
$(".txt").nextAll( )
.css(style_2);
});
</script>
</head>
<body>
<div id="wrap">
<h1>์ ์ฒด ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์</h1>
<p>๋ด์ฉ1</p>
<p class="txt">๋ด์ฉ2</p>
<p>๋ด์ฉ3</p>
<p>๋ด์ฉ4</p>
</div>
</body>
</html>

6. ์ ์ฒด ํ์ ์์ ์ ํ์
์ ์ฒด ํ์ ์์ ์ ํ์๋ ์ ํํ ์์์ ๋ชจ๋ ํ์ ์์๋ฅผ ์ ํํ๋ค.
$("์์ ์ ํ).siblings();
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ ํ์ </title>
<script src="js/jquery.js"></script>
<script>
$(function(){
var style_1 = {
"background-color":"#0ff",
"border":"2px solid #f00"
}
$(".txt").siblings( )
.css(style_1);
});
</script>
</head>
<body>
<div id="wrap">
<h1>์ ์ฒด ํ์ ์์ ์ ํ์</h1>
<p>๋ด์ฉ1</p>
<p class="txt">๋ด์ฉ2</p>
<p>๋ด์ฉ3</p>
<p>๋ด์ฉ4</p>
</div>
</body>
</html>

7. ๋ฒ์ ์ ํ ์ ์ฒด ํ/๋์ ์์ ์ ํ์
$("์์ ์ ํ).prevUntil("๋ฒ์ ์ ํ ์์ ์ ํ)
$("์์ ์ ํ).nextUntil("๋ฒ์ ์ ํ ์์ ์ ํ)
์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฒ์ ์ ํ ์์๊น์ง์ ์ ์ฒด ํ ์์๋ฅผ ์ ํํ๋ค.
์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฒ์ ์ ํ ์์๊น์ง์ ์ ์ฒด ๋์ ์์๋ฅผ ์ ํํ๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ ํ์ </title>
<script src="js/jquery.js"></script>
<script>
$(function(){
var style_1 = {
"background-color":"#0ff",
"border":"2px solid #f00"
}
$(".txt3").prevUntil(".title")
.css(style_1);
$(".txt3").nextUntil(".txt6")
.css(style_1);
});
</script>
</head>
<body>
<div id="wrap">
<h1 class="title">๋ฒ์ ์ ํ ์ ์ฒด ํ/๋์ ์์ ์ ํ์</h1>
<p>๋ด์ฉ1</p>
<p>๋ด์ฉ2</p>
<p class="txt3">๋ด์ฉ3</p>
<p>๋ด์ฉ4</p>
<p>๋ด์ฉ5</p>
<p class="txt6">๋ด์ฉ6</p>
</div>
</body>
</html>

8. ์์ ์์ ์ ํ์
$("์์ ์ ํ").parents()
$("์์ ์ ํ").parents("์์ ์ ํ")
์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์์๋ฅผ ๋ชจ๋ ์ ํํ๋ค.
์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์์ ์ค ์ ํํ ์์๋ง ์ ํํ๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ ํ์ </title>
<script src="js/jquery.js"></script>
<script>
$(function(){
$(".txt1").parents()
.css({
"border":"2px dashed #00f"
});
$(".txt2").parents("div")
.css({
"border":"2px solid #f00"
});
});
</script>
</head>
<body>
<h1 class="title">์ ํ์</h1>
<section>
<div>
<p class="txt1">๋ด์ฉ</p>
</div>
</section>
<section>
<div>
<p class="txt2">๋ด์ฉ</p>
</div>
</section>
</body>
</html>

9. ๊ฐ์ฅ ๊ฐ๊น์ด ์์ ์์ ์ ํ์
$("์์ ์ ํ").closest("์์ ์ ํ")
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ ํ์ </title>
<script src="js/jquery.js"></script>
<script>
$(function(){
$(".txt1").closest("div")
.css({
"border":"2px solid #f00"
});
});
</script>
</head>
<body>
<h1 class="title">๊ฐ์ฅ ๊ฐ๊น์ด ์์ ์์ ์ ํ์</h1>
<div>
<div>
<p class="txt1">๋ด์ฉ</p>
</div>
</div>
</body>
</html>

'ํ๋ก๊ทธ๋๋ฐ์ธ์ด > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ

ํจ๊ป ๋ณด๋ฉด ์ข์ ๊ด๋ จ ํฌ์คํ โผ
[JavaScript] jQuery๋ฅผ ์ฌ์ฉํ ๋ ์ ์ํด์ผ ํ ์ฌํญ๋ค
โญ ๋ฌธ์๊ฐ ๋ก๋๋ ํ ์คํํ์โญ jQuery ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋จผ์ ๋ก๋ํ์โญ $ ์ถฉ๋ ๋ฐฉ์ง ํ์ ์ noConflict โญ ์ด๋ฒคํธ ์ค๋ณต ๋ฐฉ์ง off()โญ AJAX ์บ์ฑ ๋ฐฉ์ง (cache: false) โญ ๋์ ์์์ ์ด๋ฒคํธ ์ ์ฉ on()โญ ์ฑ๋ฅ ์ต
awesomepossum.tistory.com
[JavaScript] Object(๊ฐ์ฒด), DOM(๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ), function() ํจ์ ๊ธฐ์ด ์ค์ต
1. Object (๊ฐ์ฒด) 2. DOM (Document Object Model)2-1. DOM(๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ) ๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ(DOM) HTML CSS JS 2-2. innerHTML vs textContent ์ค๋์ ๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ์ ๋ํด์ ๋ฐฐ์๋ณด์๋ค 2-3. querySelector vs getElementByID์์ฆ์๋
awesomepossum.tistory.com
์ค๋์ ์ ์ด์ฟผ๋ฆฌ ์ ํ์๋ก ์คํ์ผ์ด๋ ์์ฑ์ ์ ์ฉํ๋ ๋ฒ์ ์ ๋ฆฌํด๋ณธ๋ค.
์ ํ์๋ CSS๋ฅผ ์ ๋ค๋ฃจ๊ธฐ ์ํด ๊ผญ ์์์ผ ํ ์ค์ํ ๋ถ๋ถ์ด๋ค.
์ ์ด์ฟผ๋ฆฌ์์๋ $() ์๊ดํธ ์์ ๋ฌธ์ํ ๋ฐ์ดํฐ " "๋ก CSS ์ ํ์๋ฅผ ์ ๋ ฅํ๋ฉด ๋๋ค.
์ด ๋, .css() ๋ฅผ ์ฌ์ฉํ๋ฉด ์คํ์ผ์ ๋ณ๊ฒฝํ ์ ์๊ณ , .attr() ๋๋ .prop()์ ์ฌ์ฉํ๋ฉด HTML ์์ฑ์ ์กฐ์ํ ์ ์๋ค.
1. ์ ํํ ์์์ ์ง์ ํ ์คํ์ผ ์ ์ฉํ๊ธฐ
$(์์์ ํ).css(์์ฑ, ๊ฐ)
// ํน์ ์์์ ์คํ์ผ ์ ์ฉ $(".my-class").css("color", "red"); // ๊ธ์์์ ๋นจ๊ฐ์์ผ๋ก ๋ณ๊ฒฝ $("#my-id").css({ "background-color": "yellow", "font-size": "20px" }); // ๋ฐฐ๊ฒฝ์๊ณผ ๊ธ์ ํฌ๊ธฐ ๋ณ๊ฒฝ
2. ์ ํํ ์์์ ์ง์ ํ ์์ฑ ์ ์ฉํ๊ธฐ
$(์์์ ํ).attr(์์ฑ, ๊ฐ)
// ํน์ ์์์ ์์ฑ ์ถ๊ฐ ๋๋ ๋ณ๊ฒฝ $("input").attr("placeholder", "๊ฐ์ ์
๋ ฅํ์ธ์"); // placeholder ์ถ๊ฐ $(".my-link").attr("href", "https://example.com"); // ๋งํฌ ๋ณ๊ฒฝ $("#my-checkbox").prop("checked", true); // ์ฒดํฌ๋ฐ์ค ์ ํ ์ํ๋ก ๋ณ๊ฒฝ
๋ชจ๋ ์์๋ค์ํผ CSS ์คํ์ผ์ CSS ํ์ผ์ ์ ์ํด์ HTML ํ์ผ๊ณผ ๋ฐ๋ก ๊ด๋ฆฌํ๋ ๊ฒ์ด ๊ฐ์ฅ ๊ถ์ฅ๋๋ ๋ฐฉ๋ฒ์ด๋ค.
jQuery์์๋ ๋์ ์ผ๋ก ์คํ์ผ์ ๋ณ๊ฒฝํด์ผ ํ ๋๋ง .css()๋ฅผ ์ฌ์ฉํ์.
๋ค๋ง ์์ฑ์ ๊ฒฝ์ฐ, CSS๊ฐ ์๋๋ผ HTML ์์์ ์์ฑ์ด๊ธฐ ๋๋ฌธ์ jQuery์ attr() ๋๋ prop()์ ์ฌ์ฉํด์ ๋ณ๊ฒฝํ๋ ๊ฒ ์์ฐ์ค๋ฝ๋ค.
๊ธฐ๋ณธ์ ํ์ ํ ๋์ ๋ณด๊ธฐ โผ
๊ตฌ๋ถ | ์ข ๋ฅ | ์ฌ์ฉ๋ฒ | ์ค๋ช |
์ง์ ์ ํ์ |
์ ์ฒด ์ ํ์ | $("*") | ๋ชจ๋ ์์ ์ ํํ๊ธฐ |
์์ด๋ ์ ํ์ | $("#์์ด๋๋ช ") | id ์์ฑ์ ์ง์ ํ ๊ฐ ๊ฐ์ง ์์ ์ ํํ๊ธฐ | |
ํด๋์ค ์ ํ์ | $(".ํด๋์ค๋ช ") | class ์์ฑ์ ์ง์ ํ ๊ฐ ๊ฐ์ง ์์ ์ ํํ๊ธฐ | |
์์ ์ ํ์ | $("์์๋ช ") | ์ง์ ํ ์์๋ช ๊ณผ ์ผ์นํ๋ ์์๋ค๋ง ์ ํํ๊ธฐ | |
๊ทธ๋ฃน ์ ํ์ | $("์ ํ1, ์ ํ2, ์ ํ3,... ์ ํn") | ์ ํ1, ... ์ ํn์ ์ง์ ๋ ์์๋ค์ ํ๋ฒ์ ์ ํํ๊ธฐ | |
์ข ์ ์ ํ์ | $("p.txt_1") $("p#txt_1") |
<p> ์์ ์ค class ๊ฐ์ด txt_1์ธ ์์ ๋๋ id๊ฐ์ด txt_1 ์ธ ์์ ์ ํํ๊ธฐ | |
์ธ์ ๊ด๊ณ ์ ํ์ |
๋ถ๋ชจ ์์ ์ ํ์ | $("์์ ์ ํ").parent() | ์ ํํ ์์์ ๋ถ๋ชจ ์์ ์ ํํ๊ธฐ |
์์ ์์ ์ ํ์ | $("์์ ์ ํ").parents() | ์ ํํ ์์์ ์์ ์์๋ฅผ ๋ชจ๋ ์ ํํ๊ธฐ | |
๊ฐ์ฅ ๊ฐ๊น์ด ์์ ์์ ์ ํ์ | $("์์ ์ ํ").closest("div") | ์ ํํ ์์์ ์์ ์์ ์ค ๊ฐ์ฅ ๊ฐ๊น์ด <div>๋ง ์ ํํ๊ธฐ | |
ํ์ ์์ ์ ํ์ | $("๊ธฐ์ค ์์1 ํ์ ์์2") | ์ ํํ ์์์ ์ง์ ํ ํ์ ์์๋ฅผ ์ ํํ๊ธฐ | |
์์ ์์ ์ ํ์ | $("์์ ์ ํ>์์์์") | ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ๊ด๊ณ์ ์ง์ ํ ์์๋ง ์ ํํ๊ธฐ | |
์์ ์์๋ค ์ ํ์ | $("์์ ์ ํ").children() | ์ ํํ ์์์ ๋ชจ๋ ์์ ์์๋ฅผ ์ ํํ๊ธฐ | |
ํ(์ด์ ) ์์ ์ ํ์ | $("์์ ์ ํ").prev() | ์ ํํ ์์์ ๋ฐ๋ก ์ด์ ์์๋ฅผ ์ ํํ๊ธฐ | |
ํ(์ด์ ) ์์๋ค ์ ํ์ | $("์์ ์ ํ").prevAll() | ์ ํํ ์์์ ์ด์ ์์ ๋ชจ๋๋ฅผ ์ ํํ๊ธฐ | |
์ง์ ํ(์ด์ ) ์์๋ค ์ ํ์ |
$("์์ ์ ํ").prevUntil("์์๋ช ") | ์ ํํ ์์๋ถํฐ ์ง์ ํ ์์์ ์ด์ ์์๊น์ง ๋ชจ๋ ์ ํํ | |
๋์(๋ค์) ์์ ์ ํ์ | $("์์ ์ ํ").next() $("์์ ์ ํ+๋ค์ ์์") |
์ ํํ ์์์ ๋ค์ ์์๋ฅผ ์ ํํ๊ธฐ | |
๋์(๋ค์) ์์๋ค ์ ํ์ | $("์์ ์ ํ").nextAll() | ์ ํํ ์์์ ๋ค์ ์์๋ฅผ ๋ชจ๋ ์ ํํ๊ธฐ | |
์ง์ ๋์(๋ค์) ์์๋ค ์ ํ์ |
$("์์ ์ ํ").nextUntil("h2") | ์ ํํ ์์๋ถํฐ ์ง์ ํ ์์์ ๋ค์ ์์๊น์ง ๋ชจ๋ ์ ํํ๊ธฐ | |
์ ์ฒด ํ์ ์์ ์ ํ์ | $(".box_1").siblings() | class ๊ฐ์ด box_1์ธ ์์์ ํ์ ์์ ์ ์ฒด๋ฅผ ์ ํํ๊ธฐ |
์ธ์ ๊ด๊ณ ์ ํ์ ์ ๋ฆฌ
1. ๋ถ๋ชจ์์ ์ ํ์
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> </head> <body> <script> $(function(){ $("#list_1").parent() .css("border", "2px dashed #f00") .css("background", "yellow") }); </script> <h1>๋ถ๋ชจ์์ ์ ํ์</h1> <ul id="wrap"> <li>๋ฆฌ์คํธ1 <ul> <li id="list_1">๋ฆฌ์คํธ1-1</li> <!-- <ul>์ ์์์ธ <li>ํ๊ทธ --> <li>๋ฆฌ์คํธ1-2</li> </ul> </li> <li>๋ฆฌ์คํธ2</li> <li>๋ฆฌ์คํธ3</li> </ul> </body> </html>

2. ํ์์์ ์ ํ์
$("๊ธฐ์ค ์์ ์ ํ1 ์์ ์ ํ2") $("#wrap h1") // id="warp"์ธ ์์๊ฐ ๊ธฐ์ค ์์
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> </head> <body> <script> $(function(){ $("#wrap h1").css({ "background-color":"yellow", "border":"2px dashed #f00" }) }); </script> <div id="wrap"> <h1>์ธ์ ๊ด๊ณ ์ ํ์</h1> <p>๋ด์ฉ 1</p> <section> <h1>ํ์ ์์ ์ ํ์</h1> <p>๋ด์ฉ2</p> </section> </div> </body> </html>

3. ์์์์ ์ ํ์
$("์์ ์ ํ > ์์์์ ์ ํ") $("์์ ์ ํ).children("์์ ์์ ์ ํ) $("์์ ์ ํ).children()
์ ๋ ์ค์ ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ง์ ํ ์์ ์์๋ง ์ ํํ๋ค.
๋งจ ์๋ ์ฝ๋๋ ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ชจ๋ ์์ ์์๋ฅผ ์ ํํ๋ค.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> </head> <body> <script> $(function(){ $("#wrap > h1").css("border", "2px dashed #f00"); $("#wrap > section").children() .css({ "background-color":"yellow", "border":"2px solid #f00" }); }); </script> <div id="wrap"> <h1>์ธ์ ๊ด๊ณ ์ ํ์</h1> <p>๋ด์ฉ 1</p> <section> <h1>์์ ์์ ์ ํ์</h1> <p>๋ด์ฉ2</p> </section> </div> </body> </html>

4. ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์
ํ ์์ ์ ํ์๋ ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ก ์ด์ ํ์ ์์๋ง ์ ํํ๋ค.
๊ทธ๋ฆฌ๊ณ ๋์ ์์ ์ ํ์๋ ์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ก ๋ค์ ํ์ ์์๋ง ์ ํํ๋ค.
์ ํํ ์์๋ฅผ (์์ ์ ํ1) ๊ธฐ์ค์ผ๋ก ๋ฐ๋ก ๋ค์์ ์ค๋ ์ ํํ ์์(์์ ์ ํ2)๋ง ์ ํํ๋ค.
$("์์ ์ ํ").prev() $("์์ ์ ํ").next() $("์์ ์ ํ1 + ์์ ์ ํ2")
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title> ์ ํ์ </title> <script src="js/jquery.js"></script> <script> $(function(){ var style_1 = { "background-color":"#0ff", "border":"2px solid #f00" } var style_2 = { "background-color":"#ff0", "border":"2px dashed #f00" } $(".txt").prev() .css(style_1); $(".txt + p").css(style_2); $(".txt").next().next() .css(style_2); }); </script> </head> <body> <div id="wrap"> <h1>ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์</h1> <p>๋ด์ฉ1</p> <p class="txt">๋ด์ฉ2</p> <p>๋ด์ฉ3</p> <p>๋ด์ฉ4</p> </div> </body> </html>

5. ์ ์ฒด ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์
$("์์ ์ ํ).prevAll() $("์์ ์ ํ).nextAll()
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title> ์ ํ์ </title> <script src="js/jquery.js"></script> <script> $(function(){ var style_1 = { "background-color":"#0ff", "border":"2px solid #f00" } var style_2 = { "background-color":"#ff0", "border":"2px dashed #f00" } $(".txt").prevAll( ) .css(style_1); $(".txt").nextAll( ) .css(style_2); }); </script> </head> <body> <div id="wrap"> <h1>์ ์ฒด ํ(์ด์ ) / ๋์(๋ค์) ์์ ์ ํ์</h1> <p>๋ด์ฉ1</p> <p class="txt">๋ด์ฉ2</p> <p>๋ด์ฉ3</p> <p>๋ด์ฉ4</p> </div> </body> </html>

6. ์ ์ฒด ํ์ ์์ ์ ํ์
์ ์ฒด ํ์ ์์ ์ ํ์๋ ์ ํํ ์์์ ๋ชจ๋ ํ์ ์์๋ฅผ ์ ํํ๋ค.
$("์์ ์ ํ).siblings();
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title> ์ ํ์ </title> <script src="js/jquery.js"></script> <script> $(function(){ var style_1 = { "background-color":"#0ff", "border":"2px solid #f00" } $(".txt").siblings( ) .css(style_1); }); </script> </head> <body> <div id="wrap"> <h1>์ ์ฒด ํ์ ์์ ์ ํ์</h1> <p>๋ด์ฉ1</p> <p class="txt">๋ด์ฉ2</p> <p>๋ด์ฉ3</p> <p>๋ด์ฉ4</p> </div> </body> </html>

7. ๋ฒ์ ์ ํ ์ ์ฒด ํ/๋์ ์์ ์ ํ์
$("์์ ์ ํ).prevUntil("๋ฒ์ ์ ํ ์์ ์ ํ) $("์์ ์ ํ).nextUntil("๋ฒ์ ์ ํ ์์ ์ ํ)
์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฒ์ ์ ํ ์์๊น์ง์ ์ ์ฒด ํ ์์๋ฅผ ์ ํํ๋ค.
์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฒ์ ์ ํ ์์๊น์ง์ ์ ์ฒด ๋์ ์์๋ฅผ ์ ํํ๋ค.
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title> ์ ํ์ </title> <script src="js/jquery.js"></script> <script> $(function(){ var style_1 = { "background-color":"#0ff", "border":"2px solid #f00" } $(".txt3").prevUntil(".title") .css(style_1); $(".txt3").nextUntil(".txt6") .css(style_1); }); </script> </head> <body> <div id="wrap"> <h1 class="title">๋ฒ์ ์ ํ ์ ์ฒด ํ/๋์ ์์ ์ ํ์</h1> <p>๋ด์ฉ1</p> <p>๋ด์ฉ2</p> <p class="txt3">๋ด์ฉ3</p> <p>๋ด์ฉ4</p> <p>๋ด์ฉ5</p> <p class="txt6">๋ด์ฉ6</p> </div> </body> </html>

8. ์์ ์์ ์ ํ์
$("์์ ์ ํ").parents() $("์์ ์ ํ").parents("์์ ์ ํ")
์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์์๋ฅผ ๋ชจ๋ ์ ํํ๋ค.
์ ํํ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์์ ์ค ์ ํํ ์์๋ง ์ ํํ๋ค.
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title> ์ ํ์ </title> <script src="js/jquery.js"></script> <script> $(function(){ $(".txt1").parents() .css({ "border":"2px dashed #00f" }); $(".txt2").parents("div") .css({ "border":"2px solid #f00" }); }); </script> </head> <body> <h1 class="title">์ ํ์</h1> <section> <div> <p class="txt1">๋ด์ฉ</p> </div> </section> <section> <div> <p class="txt2">๋ด์ฉ</p> </div> </section> </body> </html>

9. ๊ฐ์ฅ ๊ฐ๊น์ด ์์ ์์ ์ ํ์
$("์์ ์ ํ").closest("์์ ์ ํ")
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title> ์ ํ์ </title> <script src="js/jquery.js"></script> <script> $(function(){ $(".txt1").closest("div") .css({ "border":"2px solid #f00" }); }); </script> </head> <body> <h1 class="title">๊ฐ์ฅ ๊ฐ๊น์ด ์์ ์์ ์ ํ์</h1> <div> <div> <p class="txt1">๋ด์ฉ</p> </div> </div> </body> </html>
