λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
JQuery

제이쿼리 : ν•„ν„° μ„ νƒμž

by μ½”νŒŒμΉ΄ 2022. 8. 30.
728x90

제이쿼리 : ν•„ν„° μ„ νƒμž

μ§€λ‚œ μ‹œκ°„μ— 이어 μ˜€λŠ˜μ€ ν•„ν„° μ„ νƒμžμ— λŒ€ν•˜μ—¬ μ•Œμ•„λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€. ν•„ν„° μ„ νƒμžλŠ” μ„ νƒμžμ— ':'이 뢙은 μ„ νƒμžλ₯Ό λ§ν•©λ‹ˆλ‹€.


1. κΈ°λ³Έ ν•„ν„° μ„ νƒμž

μ„ νƒμž μ’…λ₯˜ μ„€λͺ…
:even $("tr:even") tr μš”μ†Œ 쀑 짝수 ν–‰λ§Œ μ„ νƒν•©λ‹ˆλ‹€.
:odd $("tr:odd") tr μš”μ†Œ 쀑 ν™€μˆ˜ ν–‰λ§Œ μ„ νƒν•©λ‹ˆλ‹€.
:first $("td:first") 첫 번째 td μš”μ†Œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€.
:last $("td:last") λ§ˆμ§€λ§‰ td μš”μ†Œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€.
:header $(":header") ν—€λ”©(h1~h6) μš”μ†Œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€.
:eq() $("li:eq(0)") indexκ°€ 0인 li μš”μ†Œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€.
: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>
</head>
<body>
    <h1>제λͺ©1</h1>
    <h2>제λͺ©2</h2>
    <table>
        <caption>κΈ°λ³Έ ν•„ν„° μ„ νƒμž</caption>
        <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>

    <script src="https://code.jquery.com/jquery-3.6.1.min.js"
    integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
    crossorigin="anonymous"></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>
</body>
</html>

2. μžμ‹ ν•„ν„° μ„ νƒμž

μžμ‹ ν•„ν„° μ„ νƒμžλŠ” λ‹€λ₯Έ μ„ νƒμžμ— λΉ„ν•˜μ—¬ μ•½κ°„ μ–΄λ €μšΈ 수 μžˆμŠ΅λ‹ˆλ‹€. μžμ‹ ν•„ν„° μ„ νƒμž 쀑 '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>
</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>

    <script src="https://code.jquery.com/jquery-3.6.1.min.js"
    integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
    crossorigin="anonymous"></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>
</body>
</html>

3. μ½˜ν…μΈ  ν•„ν„° μ„ νƒμž

μ„ νƒμž μ’…λ₯˜ μ„€λͺ…
: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>
</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>

    <script src="https://code.jquery.com/jquery-3.6.1.min.js"
    integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
    crossorigin="anonymous"></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>
</body>
</html>

4. 폼 ν•„ν„° μ„ νƒμž

μ„ νƒμž μ’…λ₯˜ μ„€λͺ…
: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="checkbox"> μš”μ†Œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€.
: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>
    <style>
        * {
            margin: 5px;
        }
        #m1 > p:last-child {
            height: 20px;
        }
        span {
            display: inline-block;
            height: 20px;
        }
    </style>
</head>
<body>
    <div><input type="text"></div>
    <div><input type="password"></div>
    <div><button>확인</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" rows="5" disabled="disabled">javascript</textarea>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.1.min.js"
    integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
    crossorigin="anonymous"></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>
</body>
</html>
728x90

λŒ“κΈ€

κ°μ‚¬ν•©λ‹ˆλ‹€. πŸ¦™

CSS
κ΄‘κ³  쀀비쀑