내 블로그 목록

2018년 6월 4일 월요일

[jQuery] jQuery 총정리_Basic

What is jQuery?

jQuery is a lightweight, "write less, do more", JavaScript library.
The purpose of jQuery is to make it much easier to use JavaScript on your website.
jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.
The jQuery library contains the following features:
  • HTML/DOM manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX
  • Utilities
Tip: In addition, jQuery has plugins for almost any task out there.
(from: https://www.w3schools.com/Jquery/jquery_intro.asp) 


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
    <title>Document</title>
</head>
<body>
    <!-- h1{Hello}*6 -->
    <h1>Hello</h1>
    <h1>Hello</h1>
    <h1>Hello</h1>
    <h1>Hello</h1>
    <h1>Hello</h1>
    <h1>Hello</h1>
    
    <!-- div>h1>div>h2 -->
    <!-- <div>
        <h1>
            <div>
                <h2 class= "select">World</h2>
            </div>
        </h1>
    </div> -->
 
    <!-- h1.item{Header-$}*3 -->
    <h2 class="item">Header-1</h2>
    <h2 class="item select">Header-2</h2>
    <h2 class="item">Header-3</h2
 
    <!-- input[type=text] -->  <!-- c+tap = comment -->
    <input type="text"/>
    <input type="password"/>
    <input type="radio"/>
    <input type="checkbox" checked/<!-- checked -->
    <input type="file"/>
 
    <!-- select>option[value=""]*4 -->
    <select name="" id="">
        <option value="농구">농구</option>
        <option value="야구">야구</option>
        <option value="축구">축구</option>
        <option value="하키">하키</option>
    </select>
 
    <table border=1>
        <!-- (tr>td*3)*4 -->
        <tr>
            <td>번호</td>
            <td>이름</td>
            <td>조회수</td>
        </tr>
        <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>
    </table>
 
    <!-- img[src="card//1_1.png"]*4 -->
    <img src="card//1_1.png" alt="">
    <img src="card//1_2.png" alt="">
    <img src="card//1_3.png" alt="">
    <img src="card//1_4.png" alt="">
 
    <script>
 
    // 0. function 문법 ->  $(document).ready(function(){}) -> $(function(){]})
    // $(document).ready(function(){
    //     alert('First READY');
    // });
    // $(document).ready(function(){
    //     alert('Second READY');
    // });
    // $(document).ready(function(){
    //     alert('Third READY');
    // });
 
    $(function(){
        alert('First READY');
    });
    $(function(){
        alert('Second READY');
    });
    $(function(){
        alert('Third READY');
    });
 
 
    // 1. css 설정(color, background, padding etc)
    // (0). javascript
    // var tags = document.querySelectorAll('h1');
 
    // for(var i=0; i<tags.length; i++){
    //     tags[i].style.color = 'red';
    // }
  
    // (1). jquery
    $('h1').css('color','blue');
    $('h1').css('background-color','black');
    //  = $('h1').css('color','blue').css('background-color','black');
    //  = $('h1').css({'color':'blue', 'background':'pink'});
    $('h1').css('padding''10px');
    
 
    // 2.selector 사용
    $('*').css('border''5px solid red');        //* = 전체 선택자 
    $('h1').css('border''5px solid red');       //모든 h1 요소(='h1 *'/ 'h1>*')
    $('h1, p').css('border''5px solid red');    //h1, p = 모든 h1과 p들 선택
    $('div h1').css('border''5px solid red');   //div h1 = div안에 있는 h1(=div>h1)
    $('#target').css('border''5px solid red');  //#id이름 = 특정 id선택
    $('h1.item').css('background'"yellow");     //h1의 class="item"
    $('.select').css('background''green');      //class="select" 
    $('.item.select').css('background','blue');   //class = "item select" 두 클래스 
   
 
 
    // 3.속성(property)
    $('input[type=text]').val('its me'); //type이 text인 input에 값 입력 
    $('input[type=text]').css('background-color','yellow'); //type이 text인 input에 색 설정
    
    $('input[type=checkbox]').attr('checked'); //type이 checkbox인 input의 속성 "checked"로 변경 
    $('input[type=checkbox]').prop('checked'); //"true"
    $('input[type=checkbox]').prop('checked'false); //type이 checkbox인 input의 속성 checked값을 false로 변경
 
    //추가 부분: 속성 선택자
    $(':selected').val()  //"농구" -> selected된 값 출력 
    // = $('select>option:selected').val() 
    $('select>option'); //옵션 전체 출력
 
 
    // 4.fliter 사용 -> 주로, table에서 사용
    $('td').css('padding','30px');
    $('td').css('border','3px solid red');
    
    $('h1').filter(':even').css({'color':'yellow'"background":"skyblue"}); //짝수 선택
    $('h1').filter(':odd').css({'color':'blue'"background":"yellow"}); //홀수 선택
    
    $('tr').filter(':first').css('color','blue');  //첫 줄 선택
    // = $('tr:first').css('color','blue');
    $('tr').filter(':last');   //마지막 줄 선택
    $('tr').filter(':eq(0)');  //첫번째 칸 선택 
    $('tr').filter(':gt(n)');  //n번째 초과에 위치하는 문서 선택 
    $('tr').filter(':lt(n)');  //n번째 미만에 위치하는 문서 선택 
        
    $('td:nth-child(3n+1)').css('background','pink'); //1,4,7 칸 선택 
    $('td:nth-child(3n+2)').css('background','skyblue'); //2,5,8 선택 
    $('td:nth-child(3n)').css('background','yellow'); //0,3,6 선택
    
    $('h1').fliter(function(i){
        return Math.floor(Math.random()*6)%2==0;
    }).css({'color':'green'"background":"skyblue"}); //random으로 색칠하기
 
    //<-> end(): 문서 객체 선택을 한 단계 뒤로 돌림
     $('h1').css('background''Orange').filter(':even').css('color','White').end().filter(':odd').css('color','red');
    
 
    // 5.생성과 제거 
    $('h1').html();   //In console, 검색 
 
    $('h1').html("Between us");    //"Between us" 생성
    // = $('<h1></h1>').html("Between us");
    // = $('<h1></h1>').html("Between us").appendTo('body');
    // = $('<h1>Between us</h1>').appendTo('body');
 
    $('h1').html(function(index){  //문자+index 생성  
        return "its me"+index; 
    });    
 
    $('h1').remove();  //h1 제거
    $('h1').eq(0).remove();  //h1의 첫번째 제거 
    
   
    // 6.문서 객체의 이동(15.15)_2초마다 이미지 위치 변경 
    $(document).ready(function(){
        $('img').css('width',250);  //img의 크기 조정 
 
        setInterval(function() {    //함수를 2초마다 실행
            $('img').first().appendTo('body');
        },2000);
    });
 
 
    // ** console 검색 문법 
    // $('h1'); 
    //   = jQuery('h1');
    //   = $('body>h1');
    // typeof $   //타입체크
    </script>
 
</body>
</html>
cs

댓글 없음:

댓글 쓰기