• 코드:
​x
 
1
<!DOCTYPE html>
2
<html lang="ko">
3
​
4
<head>
5
    <meta charset="UTF-8">
6
    <title>jQuery Etc Traversing</title>
7
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
8
    <style>
9
        .boldFont { color: green; font-weight: bold; }
10
    </style>
11
    <script>
12
        $(function() {
13
            $("button").on("click", function() {
14
                $("li").each(function() {               // 선택한 <li>요소 집합의 각 <li>요소를 선택함.
15
                    $(this).toggleClass("boldFont");    // 각 <li>요소마다 클래스를 추가하거나 제거함.
16
                });
17
            });
18
        });
19
    </script>
20
</head>
21
​
22
<body>
23
​
24
    <h1>.each() 메소드</h1>
25
    <ul>
26
        <li>첫 번째 아이템이에요!</li>
27
        <li>두 번째 아이템이에요!</li>
28
        <li>세 번째 아이템이에요!</li>
29
    </ul>
30
    <button>클래스 추가 및 제거</button>
31
    
32
</body>
33
​
34
</html>