내 블로그 목록

2018년 6월 7일 목요일

[jQuery] jQuery 총정리_event 처리

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!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>
    <style>
        /* *{
            margin: 0px; 
            padding: 0px;
        }
        div{
            margin: 5px; padding: 3px;
            border-width: 3px;
            border-style: solid;
            border-color: black;
            border-radius: 10px;
        } */
        canvas{
            background: skyblue;
        }
        .outer{ 
            width: 200px; 
            height: 200px; 
            background:Orange; 
            padding:50px; 
            margin: 10px;
            }
        .inner{ 
            width:100%; 
            height:100%; 
            background:pink;
            }
    </style>
</head>
 
<body>
    <!-- 1. $(selector).bind(object) -->
    <!-- h1{Header-$}*3 -->
        <h1>Header-1</h1>
        <h1>Header-2</h1>
        <h1>Header-3</h1>
        
    <!-- 2. 매개변수 context 예 -->
    <!-- (div>h1{Header $}+p{Paragraph})*3\ -->
    <div>
        <h1>Header 1</h1>
        <p>Paragraph</p>
    </div>
    <div>
        <h1>Header 2</h1>
        <p>Paragraph</p>
    </div>
    <div>
        <h1>Header 3</h1>
        <p>Paragraph</p>
    </div>
   
    
    <!-- 3. 이벤트 객체의 활용 예제  -->
    <div>x=0, y=0</div>
    <!-- canvas#canvas[width=500px height=300px] -->
    <canvas id="canvas" width="500px" height="300px"></canvas>
 
    <!-- 4. trigger() 사용 예제 -->
    <h1>Start:</h1>
    <h1>Start:</h1>
 
    <!-- 5. 마우스 이벤트 예제 -->
    <div class="outer">
        <div class="inner"></div>
    </div>
 
    <!-- 6. 키보드 이벤트 예제 -->
    <div>
        <p>지금 내 생각을 맞춰봐</p>
        <h1>150</h1>
        <textarea cols="70" rows="5"></textarea>
    </div>
 
    <!-- 8. 입력 양식 이벤트_submit 이벤트 -->
    <form id="my_form">
        <table>
            <tr>
                <td>이름: </td>
                <td><input type="text" name="name" id="name"/></td>
            </tr>
            <tr>
                <td>비밀번호: </td>
                <td><input type="password" name="password" id="password"/></td>
            </tr>
        </table>
        <input type="submit" value="제출"/>
    </form>
 
    <!--  9. 입력 양식 이벤트_type=change||type=radio 경우  -->
    <form id=myForm>
    <input type="checkbox" id="all_check"/>
    <label for="">All</label>
 
    <div id="check_item">
        <input type="checkbox" value="농구"/<!-- value를 지정하지 않으면 -->
        <label>농구</label>
        <input type="checkbox" value="축구"/>
        <label>축구</label>
        <input type="checkbox" value="배구"/>
        <label>배구</label>
    </div>
    <input type="submit" value="전송"/>
 </form>
 
     <!-- 10. 저수준의 시각적 효과  -->
     <h1>OPEN & CLOSE</h1>
     <div>
         <h1>Lorem ipsum</h1>
         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
     </div>
     <h1>OPEN & CLOSE</h1>
     <div>
         <h1>Lorem ipsum</h1>
         <p>Aenean vel augue dolor, at rhoncus tortor.</p>
     </div>
 
 
    <script>
      //1. $(selector).bind(object) 예제 
        // 이벤트를 연결합니다.
        $(document).ready(function(){
            $('h1').bind('click'function(){
                $(this).html(function (index, html){
                    return html + '+';
                });
            });
        });
 
 
      //2. 매개변수 context 예제
        $(document).ready(function(){
            //이벤트를 연결합니다 
            $('div').click(function() {
                //변수를 선언합니다.
                var header = $('h1', this).text();
                var paragraph = $('p', this).text();
                //출력합니다.
                alert(header + '\n' + paragraph);
            });
        });
 
 
        //3. 이벤트 객체 활용 예제 
        $(document).ready(function(){
            var canvas = document.getElementById('canvas');
            var context = canvas.getContext('2d'); 
            //   CanvasRenderingContext2D 얻음 -> RederingContext와 그리기 함수 사용 가능 
 
            $(canvas).mousemove(function(event){   
                var position = $(this).offset();    //위치 얻어내기 
                var x = event.pageX - position.left;
                var y = event.pageY - position.top;
 
                $('div').html("x="+x+", y="+y);
 
                if(event.buttons!=1){   
                    context.beginPath();  //줄 긋기 시작 메서드
                    context.moveTo(x,y);  //처음 시작 시, 위치 잡기
                    // secondClick = true;
                }else{
                    context.lineTo(x,y); 
                    context.stroke();
                    // secondClick = false;
                }
            });
        });
        //context 객체의 beginPath(), moveTo(), lineTo(), stroke() 사용하면, 
        //canvas 객체에 선을 그릴 수 있음  
 
 
       //4. trigger() 사용 예제 
        $(document).ready(function(){
            //이벤트를 연결합니다
            //h1 한번 클릭 시, 화면에 ★ 생성  
            $('h1').click(function(){
                $(this).html(function (index, html){
                    return html + '★';
                });
            });
 
            //마지막 h1은 1초에 한번씩 자동 클릭 -> trigger()
            setInterval(function(){
                $('h1').last().trigger('click');  
            }, 5000);
        });
 
 
        //5. 마우스 이벤트 예제
        $(document).ready(function(){
            $('.outer').mouseover(function(){
                $('body').append('<h1>MOUSEOVER</h1>');
            }).mouseenter(function(){
                $('body').append('<h1>MOUSEENTER</h1>');
            });;
        })
 
 
        //6. 키보드 이벤트 예제 + 글자 수 제한 추가 
        $(document).ready(function (event){
            
            $('textarea').keydown(function(){ console.log("keydown");});
            
            $('textarea').keypress(function(){            
                console.log("keypress");});
 
            $('textarea').keyup(function(){
                //남은 글자 수를 구합니다.
                var inputLength = $(this).val().length;
                var remain = 150 -inputLength;
 
            if(remain >= 0){
                $('h1').css('color''blue');  //숫자 색 변경 
                $('h1').html(remain);   //문서 객체에 입력
            }else{
                $('h1').css('color''red');  //숫자 색 변경
                $(this).val($(this).val().substr(0,150)); //문자 수 150으로 제한(this=textarea) 
                alert("글자 수 초과");
                }
            });
        });
 
 
        //7. 윈도우 이벤트_무한 스크롤 구현 
        $(document).ready(function(){
            //스크롤 이벤트 발생 시 
            $(window).scroll(function(){
                //필요한 변수를 구합니다.
                var scrollHeight = $(window).scrollTop() + $(window).height();
                var documentHeight = $(document).height();
 
                //스크롤의 높이와 문서의 높이가 같을 때 
                if(scrollHeight == documentHeight){
                    for(var i=0; i<10; i++){
                        $('<h1>HeyHey</h1>').appendTo('body');
                    }
                }
            });
        });
 
        // //테스트를 위해 내부에 공간을 채워둡니다.
        $(document).ready(function(){
            for(var i=0; i<20; i++){
                $('<h1>Infinity Scroll</h1>').appendTo('body');
            }
        })
 
 
        //8. 입력 양식 이벤트_submit 이벤트 
        $(document).ready(function(){
            $('#my_form').submit(function(event){
                //입력 양식의 value를 가져옵니다.
                var name = $('#name').val();
                var password = $('#password').val();
 
                //출력합니다.
                alert(name+ ' : ' + password);
 
                //기본 이벤트를 제거합니다.
                event.preventDefault();
            })
        })
        
        //9. 입력 양식 이벤트_type=change||type=radio 경우 
        $(document).ready(function(){
            $('#all_check').click(function(){
                if(this.checked){
                    $('#check_item').children().attr('checked'true);
                }else{
                    $('#check_item').children().attr('checked'false);
                }
            });
        });
 
        //10. 저수준의 시각적 효과 
        $(document).ready(function(){
            $('h1').click(function(){
                $(this).next().toggle('slow',function(){
                    alert('EFFECT END!')
                });
            });
        });
 
    </script>
</body>
</html>
cs

댓글 없음:

댓글 쓰기