1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="utf-8">
    <script src="/js/jquery.js"></script>
</head>
<body>
 
    <button></button>
    <p></p>
    <div></div>
     
    <script>
 
        console.log($.makeArray($("*"))); // DOM요소 배열로 변환 
        // 또는 
        console.log($("*").get());  // 결과는 같다. 
 
        // 결과 : [html, head, meta, script, body, button, p, div, script]
 
    </script>
 
</body>
</html>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
 
</head>
<body>
    <div id="wrapper">
 
        <h1 class="h">h1</h1>
        <h2 class="h">h2</h2>
        <h3 class="h">h3</h3>
        <h4 class="h">h4</h4>
        <h5 class="h">h5</h5>
        <h6 class="h">h6</h6>
        <p>p1</p>    
        <p>p2</p>    
        <span>s1</span>
        <span>s2</span>
        <i>i1</i>
        <i>i2</i>
        <div>d1</div>
        <div>d2</div>
 
    </div>
 
<script src="lib/jquery/2.2.3/jquery.min.js"></script>
<script>
 
    // jQuery 객체는 배열과 완전히 같지는 않으며 배열의 기본 제공 함수 가 부족합니다. (예에서 .pop()와 .reverse())
 
    var h = $.makeArray($('.h')); 
    var p = $.makeArray($('p')); 
    var span = $.makeArray($('span')); 
    var i = $.makeArray($('i')); 
    // console.log($("#wrapper").find(">").get()); // makeArray메서드와 실행 결과는 같다 (배열반환)
 
    var all = h.concat(p, span, i); // 배열 합치기
    console.log(all);
    console.log(all.length);
 
    // 배열 원소 삭제 array.splice(인덱스, 인덱스부터 삭제할 원소개수)
    var el = all.splice(3, 1); 
    console.log(all);
    console.log(all.length);    
    console.log(el) // h4  // 삭제 원소 반환
 
    // 배열 중간에 원소 추가 array.splice(인덱스, 삭제할 요수의 개수(추가시 0), 추가할 원소 ...)
    all.splice(3, 0, $('div')[2]); 
    console.log(all);
    console.log(all.length);    
 
    $(all).css("color""red"); // 순수배열을 jquery배열로 변환후 jquery 메서드 적용
 
</script>
 
</body>
</html>


반응형
Posted by 힘없는염소