jQuery - click,append,submit

2020. 7. 28. 19:53jQuery

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.high_0{background: yellow;}
.high_1{background: orange;}
.high_2{background: blue;}
</style>
<script src="../js/jquery-3.5.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    //each()반복
    //1. 태그 대상
    //2. 변수 대상
    //3. 파일결과 대상
    $('h3').each(function (index) {
        $(this).addClass('high_'+index);
    });
    
    //배열변수 선언
    var arr=[
        {name:'naver',link:"http://www.naver.com"},
        {name:'daum',link:"http://www.daum.net"},
        {name:'jquery',link:"http://www.jquery.com"}
    ];
    //배열반복-> 테이블 출력
    $.each(arr,function(index,item){
//         alert(index);
//         alert(item.name);
//         alert(item.link);
            //table 태그 뒤부분에 name         link 추가하기
            $('table').append('<tr><td>'+item.name+'</td><td>'+item.link+'</td></tr>');
            
    });
    
});
 
</script>
</head>
<body>
    <h1>js_jQuery1/test7.jsp</h1>
    <table border="1">
    <tr><td>사이트 이름</td><td>사이트 주소</td></tr>
    </table>
    <h3 class="high_0">head-0</h3>
<h3 class="high_1">head-1</h3>
<h3 class="high_2">head-2</h3>
</body>
</html>
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="../js/jquery-3.5.1.js"></script>
 
<script type="text/javascript">
$(document).ready(function() {
    //이벤트  bind()   click()   mouseover()
//     $('h3').bind('click',function(){
//         alert('클릭');
//     });
    //h3 click이벤트  h3안에 내용을 click이벤트  변경
//     $('h3').click(function(){
//         $(this).html('click이벤트');
//         alert('클릭이벤트');
//         //이벤트 종료
//         $(this).unbinc();
//     });
    //h3 click 클릭했을 때 h3안에 내용을 가져와서 내용에 * 추가
    $('h3').click(function(){
        $(this).html(function(index,oldhtml){
            return oldhtml+'*';
        });
    });
    //이미지 대상에 마우스 오버했을 때 src 2.jpg 변경`
    //마우스 아웃했을 때 src 1.jpg 변경
    $('img').mouseover(function(){
        $(this).attr('src','2.jpg');
    });
    $('img').mouseout(function(){
        $(this).attr('src','1.jpg');
    });
    
    
    
});
</script>
</head>
<body>
<img src="1.jpg">
<h1>js_jQuery1/test8.jsp</h1>
<h3>head_0</h3>
<h3>head_1</h3>
<h3>head_2</h3>
</body>
</html>
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="../js/jquery-3.5.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#my_form').submit(function(){
        var id=$('#id').val();
        var pass=$('#pass').val();
//         alert(id);
//         alert(pass);
        //id가 비어있으면 "아이디 입력하세요" 포커스 focus()
        if(id==""){
            
//             alert('아이디 입력하세요.');
 
            $('#divid').html('아이디 입력하세요.');
            $('#id').focus();
        
            //submit 기능을 중지
            return false;
        }
            
        //pass가 비어있으면 "비밀번호 입력하세요"포커스 focus()
         if(pass==""){
            
//             alert('비밀번호 입력하세요.');
            $('#divpass').html('비밀번호입력하세요.');
            $('#pass').focus();
            
            return false;
        }
            //성별 선택안하면 "성별 선택하세요" 포커스
            //$('#man').is(':checked')=>true/false
//             alert($('#man').is(':checked'));
            if($('#man').is(':checked')==false&&$('#woman').is(':checked')==false){
                alert('성별을 선택하세요.');
                $('#man').focus();
                //submit 기능 중지
                return false;
            }
            
        
    });
    
    
});
</script>
</head>
<body>
<h1>js_jQuery1/test9.jsp</h1>
<form action="a.jsp" method="post" id="my_form">
아이디 :  <input type="text" name="id" id="id"><div id='divid'></div><br>
비밀번호 : <input type="password" name="pass" id="pass"><div id='divpass'></div><br>
성별 : <input type="radio" name="gender" id="man" value="남">
<input type="radio" name="gender" id="woman" value="여"><br>
<input type="submit" value="회원가입">
</form>
</body>
</html>
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="../js/jquery-3.5.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    //효과 show()  hide()   toglle()
    //slideDown()   slideUp()   slideToggle()
    //fadeIn()  fadeOut()  fadeToggle()
    
    //h2 클릭했을때 기능
    $('h2').click(function(){
        $(this).next().toggle('slow');
    });
    
    
    
});
 
</script>
</head>
<body>
<h1>js_jQuery1/test10.jsp</h1>
<h2>열고 닫기1</h2>
<div>
<h2>제목1</h2>
<p>내용1</p>
</div>
 
<h2>열고 닫기2</h2>
<div>
<h2>제목2</h2>
<p>내용2</p>
</div>
</body>
</html>
 
cs

 

확실히 몇 번 쓰다 보니 문법이 조금 익숙해졌다. 간단하게 많은 걸 할 수 있어서 좋은 거 같다.

(), {}, ; 이 세 가지를 헷갈리지 말고 잘 확인을 하도록 하자. 복습 복습!

'jQuery' 카테고리의 다른 글

jQuery - XML  (0) 2020.07.29
jQuery - 결과가져오기  (0) 2020.07.29
jQuery - Login 할 때 duplication ID check  (0) 2020.07.29
jQuery - inner_pade  (0) 2020.07.28
jQuery - 설치 및 기초 표현방식  (0) 2020.07.27