λ°μν
jQuery νν° μ νμ
μ μ΄μΏΌλ¦¬μμ μ¬μ©λλ νν° μ νμμ λν΄ μ΄ν΄λ΄
μλ€.
μ νμμ ' : 'κ° λΆμ΅λλ€.
νν° μ νμ
μ νμ μ’ λ₯ | μ νμ μ€λͺ | |
---|---|---|
:even | $("tr:even") | tr μμ μ€ μ§μ νλ§ μ ν |
:odd | $("tr:odd") | tr μμ μ€ νμ νλ§ μ ν |
:first | $("tr:first") | 첫 λ²μ§Έ td μμλ₯Ό μ ν |
:last | $(":header") | ν€λ©(h1~h6) μμλ₯Ό μ ν |
:eq() | $("li:eq(0)") | indexκ° 0μΈ li μμλ₯Ό μ ν. indexλ 0λ²μ΄ 첫 λ²μ§Έ μμμ λλ€. |
:gt() | $("li:gt(0)") | indexκ° 0λ³΄λ€ ν° li μμλ€μ μ ν |
:lt() | $("li:lt(2)") | indexκ° 2λ³΄λ€ μμ li μμλ€μ μ ν |
:not() | $("li:not(.bg)") | li μμ μ€μμ classλͺ bgκ° μλ li μμλ₯Ό μ ν |
:root | $(":root") | htmlμ μλ―Έν¨ |
:animated | $(":animated") | μμ§μ΄λ μμλ₯Ό μ ν |
μμ μ½λ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>νν° μ νμ<title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
td {
border: 1px solid #333;
}
li {
color: white;
margin: 5px;
}
</style>
<script src="jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
$("tr:even").css("background", "red");
$("tr:odd").css("background", "orange");
$("td:first").css("background", "yellow");
$("td:last").css("background", "green");
$(":header").css("background", "blue");
$("li:eq(0)").css("background", "navy");
$("li:gt(0)").css("background", "purple");
$("li:lt(3)").css("border", "4px solid gray");
$(":root").css("background", "lightgray");
(function upDown() {
$("h2").slideToggle(2000,upDown);
})();
$(":animated").css("border","4px solid darkred");
});
</script>
</head>
<body>
<h1>μ λͺ©1</h1>
<h2>μ λͺ©2</h2>
<table>
<caption>κΈ°λ³Έ νν° μ νμ</caption>
<colgroup>
<col>
<col>
<col>
</colgroup>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
</tbody>
</table>
<ul>
<li class="bg">λ΄μ©1</li>
<li class="bg">λ΄μ©2</li>
<li class="bg">λ΄μ©3</li>
<li>λ΄μ©4</li>
</ul>
</body>
</html>
μ½λν κ²°κ³Ό
μμ νν° μ νμ
: 'child'κ° λΆμ μ νμλ μμκ° μμ°¨μ μΌλ‘ λμ΄λμ΄ μμ λ μ¬μ©νλ©°, "of-type"μ΄ λΆμ μ νμλ μμκ° μμ°¨μ μΌλ‘ λμ΄λμ΄ μμ§ μμλ λμΌ μμλΌλ©΄ μ ν κ°λ₯νλ€.
μ νμ μ’ λ₯ | μ νμ μ€λͺ | |
---|---|---|
:first-child | $("span:first-child") | 첫 λ²μ§Έ span μμλ₯Ό μ ν |
:last-child | $("span:last-child") | λ§μ§λ§ span μμλ₯Ό μ ν |
:first-of-type | $("span:first-of-type") | span μμ μ€μμ 첫 λ²μ§Έ span μμλ₯Ό μ ν |
:last-of-type | $("span:last-of-type") | span μμ μ€μμ λ§μ§λ§ span μμλ₯Ό μ ν |
:nth-child() | $("span:nth-child(2)") | λ λ²μ§Έ span μμλ₯Ό μ νν¨. nth-child(2n)μ 2, 4, 6, β¦λ²μ§Έ μμλ₯Ό μ ννκ³ , nth-child(2n+1)μ 1, 3, 5, β¦λ²μ§Έ μμλ₯Ό μ νν¨. |
:nth-last-child() | $("span:nth-last-child(2)") | λ§μ§λ§μμ λ λ²μ§Έ span μμλ₯Ό μ ν |
:nth-of-type() | $("span:nth-of-type(2)") | span μμ μ€μμ λ λ²μ§Έ span μμλ₯Ό μ ν |
:nth-last-of-type() | $("span:nth-last-of-type(2)") | span μμ μ€μμ λ§μ§λ§μμ λ λ²μ§Έ span μμλ₯Ό μ ν |
:only-child | $("div > span:only-child") | divμ μμ μμμμ μ€μ§ span μμκ° νλλ§ μλ span μμλ₯Ό μ ν |
:only-of-type | $("div > span:only-of-type") | divμ μμ μμμμ span μμκ° νλλ§ μλ span μμλ₯Ό μ ν |
μμ μ½λ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>μμ νν° μ νμ<title>
<style>
span {
margin: 5px;
display: inline-block;
}
</style>
<script src="jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
$("#m1 > span:first-child").css("border", "2px solid red");
$("#m1 > span:last-child").css("border", "2px solid red");
$("#m2 > span:first-of-type").css("border", "2px solid orange");
$("#m2 > span:last-of-type").css("border", "2px solid orange");
$("#m3 > span:nth-child(1)").css("border", "2px solid yellow");
$("#m3 > span:nth-last-child(1)").css("border", "2px solid yellow");
$("#m4 > span:nth-of-type(1)").css("border", "2px solid green");
$("#m4 > span:nth-last-of-type(1)").css("border", "2px solid green");
$("#m5 > span:only-child").css("border", "2px solid blue");
$("#m6 > span:only-of-type").css("border", "2px solid navy");
});
</script>
</head>
<body>
<div id="m1">
<span>λ΄μ©1_1</span>
<span>λ΄μ©1_2</span>
<span>λ΄μ©1_3</span>
</div>
<div id="m2">
<strong>λ΄μ©2_1</strong>
<span>λ΄μ©2_2</span>
<strong>λ΄μ©2_3</strong>
<span>λ΄μ©2_4</span>
</div>
<div id="m3">
<span>λ΄μ©3_1</span>
<span>λ΄μ©3_2</span>
<span>λ΄μ©3_3</span>
</div>
<div id="m4">
<strong>λ΄μ©4_1</strong>
<span>λ΄μ©4_2</span>
<strong>λ΄μ©4_3</strong>
<span>λ΄μ©4_4</span>
<strong>λ΄μ©4_5</strong>
<span>λ΄μ©4_6</span>
</div>
<div id="m5">
<span>λ΄μ©5_1</span>
</div>
<div id="m6">
<strong>λ΄μ©6_1</strong>
<span>λ΄μ©6_2</span>
</div>
</body>
</html>
μ½λν κ²°κ³Ό
μ½ν μΈ νν° μ νμ
μ νμ μ’ λ₯ | μ νμ μ€λͺ | |
---|---|---|
:contains() | $("p:contains('html')") | p μμ μ€μμ 'html' ν μ€νΈλ₯Ό ν¬ν¨νκ³ μλ p μμλ₯Ό μ ν |
:empty | $("div:empty") | div μμ μ€μμ μμ μμκ° μλ div μμλ₯Ό μ ν |
:parent | $("span:parent") | span μμ μ€μμ μμ μμκ° μλ span μμλ₯Ό μ ν |
:has() | $("section:has(article)") | section μμ μ€μμ articleμ νμ μμλ‘ κ°μ§κ³ μλ section μμλ₯Ό μ ν |
μμ μ½λ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>μμ νν° μ νμ<title>
<style>
* {
margin: 5px;
}
#m1 > p:last-child{
height: 20px;
}
span {
display: inline-block;
height: 20px;
}
</style>
<script src="jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
$("#m1 > p:contains('html')").css("border","4px solid red");
$("#m1 > p:empty").css("border","4px solid orange");
$("#m2 > span:parent").css("border","4px solid yellow");
$("#m3 > section:has(article)").css("border","4px solid green");
});
</script>
</head>
<body>
<div id="m1">
<p>html, css, javascript</p>
<p>html5μ μΉνμ€</p>
<p></p>
</div>
<div id="m2">
<span></span>
<span>λ΄μ©1</span>
</div>
<div id="m3">
<section>
<article>λ΄μ©2_1</article>
</section>
<section>
<div>
<article>λ΄μ©2_2</article>
</div>
</section>
</div>
</body>
</html>
μ½λν κ²°κ³Ό
νΌ νν° μ νμ
μ νμ μ’ λ₯ | μ νμ μ€λͺ | |
---|---|---|
:text | $("input:text") | <input type="text"> μμλ₯Ό μ ν |
:password | $("input:password") | <input type="password"> μμλ₯Ό μ ν |
:image | $("input:image") | <input type="image"> μμλ₯Ό μ ν |
:file | $("input:file") | input type="file"> μμλ₯Ό μ ν |
:button | $(":button") | <input type="button">, <button> μμλ₯Ό μ ν |
:checkbox | $("input:checkbox") | <input type="techeckboxxt"> μμλ₯Ό μ ν |
:radio | $("input:radio") | <input type="radio"> μμλ₯Ό μ ν |
:submit | $("input:submit") | <input type="submit"> μμλ₯Ό μ ν |
:reset | $("input:reset") | <input type="reset"> μμλ₯Ό μ ν |
:input | $(":input") | λͺ¨λ <input> μμλ₯Ό μ ν |
:checked | $("input:checked") | <input> μμμ checked μμ±μ΄ μλ μμλ₯Ό μ ν |
:selected | $("option:selected") | <option> μμμ selected μμ±μ΄ μλ μμλ₯Ό μ ν |
:focus | $("input:focus") | νμ¬ <input>μ ν¬μ»€μ€κ° μλ μμλ₯Ό μ ν |
:disabled | $("input:disabled") | <input> μμμ disabled μμ±μ΄ μλ μμλ₯Ό μ ν |
:enabled | $("input:enabled") | <input> μμ μ€ disabledκ° μλ μμλ₯Ό μ ν |
μμ μ½λ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>νΌ νν° μ νμ<title>
<script src="jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
$("input:text").css("background","red");
$("input:password").css("background","orange");
$(":button").css("background","yellow");
$("input:checked + label").css("background","green");
$("option:selected").css("color","blue");
$("textarea:disabled").css("background","pink");
});
</script>
</head>
<body>
<div><input type="text"></div>
<div><input type="password"></div>
<div><button>νμΈ</div>
<div><input type="checkbox" id="css" checked="checked"><label for="css">css</label></div>
<div>
<select>
<option>κ³Όλͺ©μ ν</option>
<option>javascript</option>
<option selected="selected">jQuery</option>
</select>
<textarea cols="20" id="" rows="5" disabled="disabled">javascript</textarea>
</div>
</body>
</html>
μ½λν κ²°κ³Ό
κ°μμ± νν° μ νμ
μ νμ μ’ λ₯ | μ νμ μ€λͺ | |
---|---|---|
:hidden | div:hidden | div μμ μ€ hiddenμΈ μμλ₯Ό μ ν |
:visible | div:visible | div μμ μ€ visible μμλ₯Ό μ ν |
μμ μ½λ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>κ°μμ± νν° μ νμ<title>
<style>
#vis {
display: none;
}
</style>
<script src="jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
console.log($("div:hidden").text());
$("div:visible").css("background","orange");
});
</script>
</head>
<body>
<div id="vis">λ΄μ©1</div>
<div>λ΄μ©2</div>
</body>
</html>
μ½λν κ²°κ³Ό
λ΄μ©1μ κ²μ¬(F12)μ μ½μμ°½μμλ§ νμΈ κ°λ₯νλ€.
λ°μν
'jQuery' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[jQuery] ν΄λμ€ κ΄λ ¨ λ©μλ μμ보기 (6) | 2022.09.03 |
---|---|
[JavaScript] jQuery(μ μ΄μΏΌλ¦¬) - νμ μ νμ (3) | 2022.08.30 |
[JavaScript] jQuery(μ μ΄μΏΌλ¦¬) - μμ± μ νμ (4) | 2022.08.30 |
[JavaScript] jQuery(μ μ΄μΏΌλ¦¬) - κΈ°λ³Έ μ νμ (4) | 2022.08.30 |
[JavaScript] jQuery(μ μ΄μΏΌλ¦¬) μμ보기 (5) | 2022.08.30 |
λκΈ