-
jQeury ์ ์ด์ฟผ๋ฆฌ ๊ธฐ๋ณธ๋ฌธ๋ฒ, ์ ํ์, ํํฐJavaScript 2020. 9. 24. 15:55
๐ฏ ์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ์ด์ฟผ๋ฆฌ๋ฅผ ์์๋ณธ๋ค.
์ ์ด์ฟผ๋ฆฌ(jQuery)
- ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋(ํฌ๋ก์ค ๋ธ๋ผ์ฐ์ง) ์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ๋ฌด๋ฃ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ๋ ์ ๊ทผ, ์ด๋ฒคํธ ์ฐ๊ฒฐ, Ajax ์ ํ๋ฆฌ์ผ์ด์ ์ฝ๊ฒ ๊ตฌํ ๊ฐ๋ฅ
- css ์ ํ์๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํด์ ๋์ ํ๋ฉด์ฒ๋ฆฌ ๊ฐ๋ฅ
์ธํ ๋ฐฉ๋ฒ
ํํ์ด์ง์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ค์ด๋ฐ๊ฑฐ๋ cdn์ ์ฌ์ฉํ๋ค.
-
CDN ์ฌ์ฉ๋ฐฉ๋ฒ
html์ ์ ์ด์ฟผ๋ฆฌ cdn ์คํฌ๋ฆฝํธ๋ฅผ ์ฝ์ ํ๋ค.
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
-
์ ์ด์ฟผ๋ฆฌ ๋ค์ด๋ก๋ ํ ์ดํด๋ฆฝ์ค ee ๋ค์ด๋ด๋ฏน ํ๋ก์ ํธ์์ ์ฌ์ฉ๋ฐฉ๋ฒ
์ ์ด์ฟผ๋ฆฌ ํํ์ด์ง ์ ์ : https://jquery.com/
compressed๋ ์์ถ๋ฒ์ ์ด๊ณ uncompressed๋ ๋น์์ถ ๋ฒ์ ์ด๋ค. slim์ ๋น๋๊ธฐํต์ ์ด ๋น ์ง๊ฑฐ๋ผ ์์ธ๊ฑฐ๋ค.
์ฉ๋์ด ๋ค์ฏ ๋ฐฐ ์ด์ ์ฐจ์ด๋๋ค. ์ํ๋ ํ์ผ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
์น์ปจํ ํธ ํด๋ ๋ฐ์ ์ ์ด์ฟผ๋ฆฌ ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
์๋ฐ์คํฌ๋ฆฝํธ ์๋์์ฑ ํ๋ฌ๊ทธ์ธ Tern์ด ์ค์น๋์๋ค๋ฉด ํดํ๋ก์ ํธ๋ก convert ํด์ฃผ๊ธฐ
<script type="text/javascript" src="../jquery-3.5.1.min.js"></script>
์คํฌ๋ฆฝํธ ํ๊ทธ๋ฅผ ์ฌ์ฉํด ์์ ๋๋ ํ ๋ฆฌ์ ์ ์ด์ฟผ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ
์ ์ด์ฟผ๋ฆฌ ๊ธฐ๋ณธ ๋ฌธ๋ฒ
$(selector).action();
$๋ฌธ์๋ jQuery๋ฅผ ์ ์ธํ๊ฑฐ๋ ์ ๊ทผํ ๋ ์ฌ์ฉํ๋ค. $๋ฌธ์๋ ‘jQuery’ ๋ฌธ์์ ๋ณ์นญ(alias)์ด๋ค.
jQuery(selector).action();
๊ณผ ๋์ผํ๋ค. action() ๋ HTML์์ ํน์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋ ์คํ๋๋ ๋ฉ์๋์ด๋ฉฐ ์์๋ ์ด๋ฒคํธํธ๋ค๋ฌ๊ฐ ๋ค์ด๊ฐ๋ค.$(document).ready(function () { });
์ ์ด์ฟผ๋ฆฌ์ ๊ธฐ๋ณธ ์ฝ๋ ํํ์ด๋ค. jQuery๋ก ์์ํ๋ ๋ชจ๋ ํ์ด์ง๋ ready() ํจ์๋ก ์์ํ๋๋ฐ ๋ฌธ์ ์ค๋น๊ฐ ์๋ฃ๋๋ฉด ์ธ์๋ก ์ ๋ฌ๋ ํจ์๋ฅผ ์คํํ๋ผ๋ ์๋ฏธ์ด๋ค. ์ ์ด์ฟผ๋ฆฌ ์ด๋ฒคํธ ๋ฉ์๋ ์ค ํ๋๋ก ์ฌ๋ฌ๋ฒ ๋ฐ๋ณต ์ฌ์ฉ ๊ฐ๋ฅํ๋ค. ์์ผ๋ก ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ready์ ์ต๋ช ํจ์ ์์์ ์์ฑํ๋ค.
$(document).ready(function () { console.log('hello~'); }); $(function (){ console.log('ready2'); });
$๋ถํฐ ready๊น์ง๋ฅผ ๊ทธ๋ฅ $ ํ๋๋ก ๊ฐ๋จํ๊ฒ ํํํ ์ ์๋ค.
์ ์ด์ฟผ๋ฆฌ ๊ธฐ๋ณธ ์ ํ์
$("*") : DOM์ ๋ชจ๋ Element ์ ํ $("tag") : ์ง์ ๋ ํ๊ทธ์ ์ผ์นํ๋ ๋ชจ๋ Element ์ ํ $("#id") : ์ง์ ๋ ์์ด๋์ ์ผ์นํ๋ ๋ชจ๋ Element ์ ํ $(".class") : ์ง์ ๋ ํด๋์ค์ ์ผ์นํ๋ ๋ชจ๋ Element ์ ํ $("tag1,tag2") : ์ง์ ๋ ์ฌ๋ฌ ํ๊ทธ๋ค๊ณผ ์ผ์นํ๋ ๋ชจ๋ Element ์ ํ
1. ์ ์ฒด ์ ํ์ ( * )
- CSS์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ ํ์๋ ์ ์ฒด ์ ํ์์ธ Wildcard Selector ์ด๋ค.
- HTML ํ์ด์ง์ ์๋ ๋ชจ๋ ๋ฌธ์ ๊ฐ์ฒด๋ฅผ ์ ํํ๋ค.
$(document).ready(function(){ $("*").css("color","red");//์คํ์ผ ์ง์ , ์ ์ฒด ์ ํ });
2. ํ๊ทธ ์ ํ์ ( ํ๊ทธ๋ช )
- ํ๊ทธ ์ ํ์๋ ์ง์ ํ ํ๊ทธ๋ง ์ ํํ๋ ์ ํ์ ์ด๋ค.
- ํ๊ทธ์ ์ด๋ฆ์ ๋ช ์์ ์ผ๋ก ์ฌ์ฉํ๋ค.
- ํ๋ ์ด์์ ํ๊ทธ ์ ํ์ ์ฌ์ฉ์ ์ฝค๋ง(,) ์ด์ฉ.
<body> <input type="text" value="aaaa"/> <p>Hello</p> <p>Hello2</p> <div>World</div> <div>world2</div> </body>
$(function(){ $("p").css("color","red"); var x = $("p"); console.log("x======" , x); console.log(x[0],x[1],x.length); console.log(x[0].innerText,x[1].innerText); console.log("์ฒซ๋ฒ์งธdiv๊ฐ", $("div")[0].innerText);//์ฃผ์ console.log("๋๋ฒ์งธdiv๊ฐ", $("div")[1].innerText);//์ฃผ์ $("div")[1].innerText="test"; console.log($("div")[0]); $(x[1]).css("color","blue"); });
ํ๊ทธ๋ฅผ ๋ณ์ x์ ์ ์ฅํด์ 0๋ฒ์งธ, ์ฒซ๋ฒ์งธ ์๋ฆฌ์ pํ๊ทธ๋ฅผ ์ ํํด ์ ์ดํ ์ ์๋ค.
3. ์์ด๋ ์ ํ์ ( #id๊ฐ )
- ํน์ ํ id ์์ฑ์ ๊ฐ์ง๊ณ ์๋ ๋ฌธ์ ๊ฐ์ฒด ์ ํํ๋ ์ ํ์.
- getElementById() ์ ๋์ผํ ๊ธฐ๋ฅ.
<p id="a">Hello</p> <p id="a2">Hello2</p> <div id="b">World</div> <span id="c">test</span>
$(function(){ $("#a2").css("color", "red"); var a2 = $("#a2"); console.log(a2); })
console.log($("#b")); console.log($("#b").innerText); console.log($("#b").text());
ํ๊ทธ์ ํ๊ทธ ์ฌ์ด์ ๊ธ์๋ด์ฉ์ ๊ฐ์ ธ์ฌ ๋ ์ ์ด์ฟผ๋ฆฌ์์ text() ํจ์๋ฅผ ์ฌ์ฉํ๋ค. innerText๋ฅผ ์ฌ์ฉํ๋ฉด undefined๋ฅผ ๋ฐํํ๋ค.
$("#c").text("aaaaaa");
ํ ์คํธ ๋ด์ฉ ์์ฒด๋ฅผ ์๋ก ๋ฐ๊ฟ๋ ์ ์ด์ฟผ๋ฆฌ๋ก ๊ฐ์ฒด ์ ๋ ํ๊ณ ํ ์คํธ ํจ์์์ ๋ด์ฉ์ ์ง์ ํ๋ค.
4. ํด๋์ค ์ ํ์ ( .ํด๋์ค๋ช )
- ํน์ ํ class ์์ฑ ๊ฐ์ง ๋ฌธ์ ๊ฐ์ฒด๋ฅผ ์ ํํ๋ ์ ํ์
- getElementsByClassName()๊ณผ ๋์ผํ ๊ธฐ๋ฅ. ( IE9 ๋ถํฐ์ง์)
<body> <p class="notMe">div class="notMe"</p> <p class="myClass">div class="myClass"</p> <div class="myClass">div class="myClass"</div> </body>
$(function () { $(".myClass").css("color", "red"); console.log("์ฒซ๋ฒ์งธ myClass>>", $(".myClass").first().text()); console.log("๋๋ฒ์งธ myClass>>", $(".myClass").last().text()); });
jQuery ๊ณ์ธต ์ ํ์ ( hierarchy )
1. ์์ ์ ํ์
- ์์์ ์ ํํ๋ ์ ํ์
- ‘๋ถ๋ชจ > ์์’์ ํํ๋ก ์ฌ์ฉ
<div> <a href="">daum</a> <a href="">naver</a> <p>test</p> <p> <a href="">Google</a> </p> </div>
$(function () { //์์๋ง ์ ํ $("div>a").css("color","red"); //์์(์์ํฌํจ) $("div a").css("font-size","40px"); })
2. ์์ ์ ํ์
- ์์์ ์ ํํ๋ ์ ํ์ ( ์์ ํฌํจ )
- '์์A ์์B’์ ํํ๋ก ์ฌ์ฉํ๋ค.
<form action=""> <div>๊ธฐ๋ณธ ํผ</div> <label for="name"> ๊ธฐ๋ณธํผ์ ์์ </label> <input name="name" id="name"> <fieldset> <label for="newsletter">๊ธฐ๋ณธํผ์ ์์,ํ๋์ ์ ์์</label> <input name="newsletter" id="newsletter"> </fieldset> </form> ๊ธฐ๋ณธํผ ํ์ : <input name="none">
(function () { $("form input").css("border","2px dotted blue"); $("form fieldset input").css("background-color","yellow"); })
3. ์ธ์ ํ ํ์ ์์ ์ ํ์
- ๊ฐ์ ๋ ๋ฒจ(Level)์ ํ์ ์์๋ค ์ค์์ ๋ฐ๋ก ๋ค์ ์ธ์ ํ ํ์ ์์ ์ ๊ทผ.
- ‘์์A + ์์B’์ ํํ๋ก ์ฌ์ฉํ๋ค.
$(document).ready(function() { //pํ๊ทธ ๋ฐ๋ก ๋ค input ํ๊ทธ ๋ฐํ $("label+input").css("color", "red").val("Labeled!!"); });
4. ๋ชจ๋ ํ์ ์์ ์ ํ์
- ๊ฐ์ ๋ ๋ฒจ(Level)์ ํ์ ์์๋ค ์ค์์ ๋ค์ ๋ชจ๋ ํ์ ์์ ์ ๊ทผ.
- ‘์์A ~ ์์B’์ ํํ๋ก ์ฌ์ฉํ๋ค.
$(document).ready(function() { //pํ๊ทธ ๋ฐ๋ก ๋ค ๋ชจ๋ a ํ๊ทธ $("p~a").css("font-size", "40px"); });
jQuery ์์ฑ ์ ํ์
์์[์์ฑ]
<span>hello</span><br> <a href="www kkk xxx">www kkk xxx</a><br> <a href="https://daum.net">๋ค์</a><br> <a href="https://naver.com">๋ค์ด๋ฒ</a><br> <a href="https://www.google.com">๊ตฌ๊ธ</a><br> <a href="https://haha.net">hahaha</a> </body>
//1. [href] $("a[href]").css("color","red").css("font-size","40px");
์์[์์ฑ=“๊ฐ”] , [์์ฑ != “๊ฐ”]
//2.[href='๊ฐ'] $("a[href='https://daum.net']").css("color","red"); //3.[href!='๊ฐ'] $("a[href!='https://daum.net']").css("color", "red");
์์[์์ฑ^=“๊ฐ”] , [์์ฑ $= “๊ฐ”]
//4. [href^='https']" ์์ฑ๊ฐ์ผ๋ก ์์ํ๋ $("a[href^='https']").css("color", "red"); //5. [href$='๊ฐ'] ์์ฑ๊ฐ์ผ๋ก ๋๋๋ $("a[href$='.net']").css("color", "red");
์์[์์ฑ*=“๊ฐ”] , [์์ฑ ~= “๊ฐ”]
//6.[href*='๊ฐ'] ํน์ ๊ฐ์ ํฌํจํ๋ $("a[href*='www']").css("color", "red"); //7. [href~='๊ฐ'] ๋จ์ดํํ๋ก ํฌํจํ๋ $("a[href~='www']").css({'color':'red', 'background-color':'yellow'});
jQuery ํํฐ ์ ํ์
๋ค์ํ ๋ฐฉ์์ผ๋ก ์ํ๋ ์์๋ฅผ ๊ฑธ๋ฌ๋ด๋ ์ญํ ์ ํ๋ค. : ๊ธฐํธ๋ฅผ ์์ ๋ถ์ฌ ์ฌ์ฉํ๋ฉฐ ๋๋ถ๋ถ ์ผ๋ฐ ์ ํ์์ ๊ฐ์ด ์ฌ์ฉํ์ง๋ง ๋จ๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
1. ๊ธฐ๋ณธํํฐ
:animated
์ ๋๋ฉ์ด์ ์ด ๋์์ค์ธ ๋ชจ๋ ์์ ๋ฐํ
run ๋๋ฅด๋ฉด ์ค๊ฐ ๋ฐ์ค ํญ์ด ๋ฐ๋๋ค.
<button id="run">run</button> <div></div> <div id="ani"></div> <div></div>
$(function() { $("#run").click(function() { $(":animated").toggleClass('toggleWidth'); }); function animateIt() { $("#ani").slideToggle("slow", animateIt); } animateIt(); })
<style type="text/css"> div { background: red; border: 1px solid blue; width: 70px; height: 70px; margin: 0 3px; float: left; } .toggleWidth { width: 40px; } </style>
๋ฒํผ์ ํด๋ฆญ ์ด๋ฒคํธ๋ฆฌ์ค๋๋ฅผ ์ถ๊ฐํด์ ์ ๋๋ฉ์ด์ ๋์์ค์ธ ์์์ ํ ๊ธ ํด๋์ค๋ฅผ ๋ถ์ฌํ๋ค. ๋ฒํผ์ ํด๋ฆญํ ๋๋ง๋ค ํด๋์ค๋ฅผ ๋ฃ์๋ค๊ฐ ๋บ๋ค๊ฐ ํ ์ ์๋ค.
slideToggle()
์ ํด๋น ์์์ ์๋ก ์ฌ๋ผ๊ฐ๊ฑฐ๋ ์๋๋ก ๋ด๋ ค๊ฐ๋ ์ฌ๋ผ์ด๋ ํจ๊ณผ๋ฅผ ๋ฒ๊ฐ์๊ฐ๋ฉด์ ์ค๋ค. slow๋ ๋๋ฆฌ๊ฒ ๋์ํ๋ผ๋ ์์ฝ์ด์ด๋ค.css๋ฅผ ๋ณด๋ฉด ๊ธฐ๋ณธ ๋๋น๊ฐ 70์ด์๋ค๊ฐ ํ ๊ธํด๋์ค๊ฐ ์์ฑ๋๋ฉด 40์ผ๋ก ๋ณํ๋ค.
:eq(index)
- ์ง์ ํ index ๊ฐ์ ํด๋นํ๋ ์์๋ฅผ ๋ฐํ.
- ์์๊ฐ ์ง์ ๋ ๊ฐ๋ฅ. ( ๋ง์ง๋ง ์์๋ถํฐ ์์ )
<table border="1"> <tr><td>TD 0</td><td>TD 1</td><td>TD 2</td></tr> <tr><td>TD 3</td><td>TD 4</td><td>TD 5</td></tr> <tr><td>TD 6</td><td>TD 7</td><td>TD 8</td></tr> </table>
$(function() { $('td:eq(2)').css("background-color", "green"); $('td:eq(-1)').css("background-color","green"); })
:even ๋ฐ :odd
- ์ง์ ๋ฒ์งธ , ํ์ ๋ฒ์งธ ํด๋นํ๋ ์์๋ฅผ ๋ฐํ.
- ์ค์ ์ง์ ๋ฒ์งธ๊ฐ ์๋ index์ ๊ฐ์ผ๋ก ์ฒ๋ฆฌ. ( index๋ 0๋ถํฐ)
<table border="1"> <tr><td>์ธ๋ฑ์ค 0</td></tr> <tr><td>์ธ๋ฑ์ค 1</td></tr> <tr><td>์ธ๋ฑ์ค 2</td></tr> <tr><td>์ธ๋ฑ์ค 3</td></tr> </table>
$(function() { $('tr:even').css("background-color", "#bbf"); $('tr:odd').css("font-size","20px"); })
:first ๋ฐ :last
- ์ฒซ ๋ฒ์งธ , ๋ง์ง๋ง์ ํด๋นํ๋ ์์๋ฅผ ๋ฐํ.
- :first๋ :eq(0)๊ณผ ๋์ผํ๊ณ :last๋ :eq(-1)๊ณผ ๋์ผ.
$(function() { $('tr:first').css("background-color", "#bbf"); $('tr:last').css("font-style","italic"); })
:gt(index) ๋ฐ :lt(index)
- ์ง์ ๋ index๋ณด๋ค ํฐ ์์(greater than) ๋ฐ ์์ ์์(less than) ๋ฐํ.
- ์์๊ฐ ์ง์ ๊ฐ๋ฅ
$(function() { $('td:gt(4)').css("background-color", "yellow"); $('td:lt(-2)').text("gt(-2)"); })
<table border="1"> <tr><td>TD 0</td><td>TD 1</td><td>TD 2</td></tr> <tr><td>TD 3</td><td>TD 4</td><td>TD 5</td></tr> <tr><td>TD 6</td><td>TD 7</td><td>TD 8</td></tr> </table>
:not(selector)
- selector์ ์ผ์นํ์ง ์๋ ๋ชจ๋ ์์ ๋ฐํ.
์ธํ์์ checkedํ ์์์ not์ ์ ํํ๋ฉด a,b๊ฐ ์ฒดํฌ๋๋ค.
$(function () { $("input:not(:checked)+span").css("background-color","red"); })
<body> <div> <input type="checkbox" name="a"> <span>ํ๊ธธ๋</span> </div> <div> <input type="checkbox" name="b"> <span>์ด์์ </span> </div> <div> <input type="checkbox" name="c" checked="checked"> <span>์ ๊ด์</span> </div> </body>
:focus
- ํ์ฌ ํฌ์ปค์ค ๋ฐ์ ์์ ๋ฐํ
$(function () { $("input").click(function(){ $("input:focus").css("background-color","yellow"); $("input:not(:focus)").removeAttr("style"); }); })
<body> <input type="text" value="one"> <input type="text" value="two"> <input type="text" value="three"> </body>
2. Child Filter
:first-child ๋ฐ :last-child
-
์ง์ ๋ ์์์ ๋ถ๋ชจ ์์๋ฅผ ๋์์ผ๋ก ์ฒซ ๋ฒ์งธ ์์ , ๋ง์ง๋ง ์์ ๋ฐํ.
$(function() { //์ฒซ๋ฒ์งธ ์์ var x = $(":first-child"); console.log(":first-child", x); //์ง์ ๋ ul์ ๋ถ๋ชจ๊ธฐ์ค var x = $("ul:first-child"); console.log("ul:first-child",x); //ul์ ์ฒซ๋ฒ์งธ ์์ var x = $("ul > :first-child"); console.log("ul > :first-child", x); //ul์ ๋ถ๋ชจ๊ธฐ์ค var x= $("li:last-child"); console.log("li:last-child", x); });
:nth-child(index) , :nth-child(2n)
- ์ง์ ๋ ์์์ ๋ถ๋ชจ ์์๋ฅผ ๋์์ผ๋ก ์ผ์นํ๋ index์ ํด๋นํ๋ ์์ ๋ฐํ. ( index๋ 1๋ถํฐ )
- ์์์ ํ๋ eq๋ ๋ค๋ฅด๊ฒ ์๋ ์ธ๋ฑ์ค๊ฐ 1๋ฒ๋ถํฐ ์์ํ๋ค.
<div> <ul> <li>aaa</li> <li>bbb</li> <li>ccc</li> </ul> </div> <div> <ul> <li>aaa</li> </ul> </div> <div> <ul> <li>aaa</li> <li>bbb</li> <li>ccc</li> </ul> </div>
$(function () { $("li:nth-child(2)").css("text-decoration", "underline"); });
:nth-child(even) ํํฐ ๋ฐ :nth-child(odd)
- ์ง์ ๋ฒ์งธ ์์ ๋ฐ ํ์ ๋ฒ์งธ ์์ ๋ฐํ. ( index๋ 1๋ถํฐ ):๋ถ๋ชจ์์๊ธฐ์ค
$(function () { $("li:nth-child(even)").css("font-size", "30px"); $("li:nth-child(odd)").css("background-color", "green"); });
<div> <ul> <li>aaa</li> <li>bbb</li> <li>ccc</li> </ul> </div> <div> <ul> <li>aaa</li> </ul> </div> <div> <ul> <li>aaa</li> <li>bbb</li> <li>ccc</li> </ul> </div>
:only-child
- ๋ถ๋ชจ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋๋ง ์กด์ฌํ๋ ์์ ์์๋ฅผ ๋ฐํ.
$(function () { $("li:only-child").css("font-size", "30px"); });
3. Form Filter
:button ํํฐ ๋ฐ :enable ,:disable
- input ํ๊ทธ ์ค์์ type์์ฑ์ด button ๋๋
- ํ์ฑํ ๋๋ ๋นํ์ฑํํ ์ ์๋ค.
$(function () { $(":button").css("border", "3px solid red"); $("#disable").click(function(){ $("input[type='text']:enabled").removeAttr("enabled"); $("input[type='text']:enabled").attr("disabled","disabled"); }); $("#enable").click(function(){ $("input[type='text']:disabled").removeAttr("disabled"); $("input[type='text']:disabled").attr("enabled","enabled"); }); $("form").submit(function (event) { event.preventDefault(); }); });
<fieldset> <input type="text" id="first"> <input type="text" id="second" disabled="disabled"> <input type="button" value="enable" id="enable"> <button id="disable">disable</button> </fieldset>
:checkbox ํํฐ ๋ฐ :checked
- type์์ฑ์ด checkbox ์์ ๋ฐํ. ( type=“checkbox”์ ๋์ผ )
- ์์ฑ์ด checked ๋๋ selected๋ ์์ ๋ฐํ.( checkbox,radio,select ํ๊ทธ์ ์ฌ์ฉ )
<body> ์ฌ๊ณผ<input type="checkbox" checked="checked" value="์ฌ๊ณผ"><br> ๋ฐฐ<input type="checkbox" value="๋ฐฐ"><br> ๋ฐ๋๋<input type="checkbox" value="๋ฐ๋๋"><br> <input type="text"> </body>
$(function () { $("input:checkbox").wrap("<span></span>").parent().css("border", "3px dotted red"); $(":text").val($("input:checked").val()); });
4. Content Filter
:contains(text)
- ์ง์ ํ text ๋ฌธ์์ด์ด ์กด์ฌํ๋ ํด๋น ์์๋ฅผ ๋ฐํ.
- ๋์๋ฌธ์ ๊ตฌ๋ณ.
- ๋์ ์์ ๋ฐ ํ์ ์์๊น์ง ๊ฒ์.
<div>John Resig</div> <div>wokajslf woiesk</div> <div>dsjaem Johnxj</div> <div>wjkj jo</div> <div> <span>dlJohn</span> </div>
(function () { $("div:contains('John')").css('text-decoration', 'underline'); });
:empty
- ์์์์๊ฐ ์กด์ฌํ์ง ์๊ณ ๋ด์ฉ์ด ๋น์ด์๋ ์์ ๋ฐํ.
- ํ๊ทธ ๋ฑ์ด :empty ํํฐ ์ ํ์์ ์ํด ์ ํ๋๋ ๋ํ์ ์ธ ์์์ด๋ค.
$(function () { $("td:empty").text("๋น ๋ฌธ์์ด"); });
:has(selector)
์ง์ ๋ selector๋ฅผ ํฌํจํ๋ ๋ชจ๋ ์์๋ฅผ ๋ฐํ.
$(function () { $("div:has(p)").css("background-color", "green"); });
:parent
์์ ์์๋ฅผ ๊ฐ์ง๊ณ ์๊ฑฐ๋ ํ ์คํธ(๋ด์ฉ) ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์์ ๋ฐํํ๋ค. empty ์ ๋ฐ๋๋๋ ์ ํ์.
$(function () { $("td:parent").text("์๋ค!!"); });
๋ฐ์ํ'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
jQuery ์ ์ด์ฟผ๋ฆฌ ์ด๋ฒคํธ, Ajax ๋น๋๊ธฐ ์ฒ๋ฆฌ (0) 2020.09.25 ์๋ฐ์คํฌ๋ฆฝํธ ์ด๋ฒคํธ ํธ๋ค๋ง ์ฐ์ต2 (0) 2020.09.13 ์๋ฐ์คํฌ๋ฆฝํธ ์ด๋ฒคํธ ํธ๋ค๋ง ์ฐ์ต1 (0) 2020.09.11 ์๋ฐ์คํฌ๋ฆฝํธ form ๊ฐ์ฒด, ์ด๋ฒคํธ ํธ๋ค๋ง (3) 2020.09.10 ์๋ฐ์คํฌ๋ฆฝํธ DOM ๊ฐ์ฒด ์ ์ด (0) 2020.09.09