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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
 
<style type="text/css">
    filedset, legend, th, td, label, input, select {
        font-size:  12px;
        color: #555;
    }
    
    /* 회원정보입력 */
    #addForm {
        width: 350px;
        margin: 10px auto;
        border: 1px solid gray;
    }
    
    #addForm legend { font-weight: bold; }
    #addForm label {
        display: inline-block; 
        width: 100px;
        text-align: right;
        margin-right: 5px;
    }
    #addForm input { border: 1px solid gray; }
    #addForm #name { width: 80px; }
    #addForm #tel { width: 180px; }
    
    /* 테이블 */
    #tbl1 {
        width: 377px;
        margin: 10px auto;
        border: 1px solid gray;
        border-collapse: collapse; /* 병합 */ 
    }
    #tbl1 th, #tbl1 td {
        border:  1px solid gray;
        padding: 4px;
    }
    #tbl1 th { background-color: #ddd; }
    #tbl1 th:nth-child(1) { width: 50px; } /* 번호 */
    #tbl1 th:nth-child(2) { width: 80px; } /* 이름 */
    #tbl1 th:nth-child(3) { width: 50px; } /* 나이  */
    #tbl1 th:nth-child(4) { width: 145px; }/* 연락처 */
    #tbl1 th:nth-child(5) { width: 50px; }
    
    #tbl1 td { text-align: center;}
    
</style>
 
<script type="text/javascript">
    
    var txtname;
    var age;
    var tel;
    var tbody1;
    var no = 1;//번호용도로 쓸
    
    function init() {
        //alert(typeof(name));
        //미리 컨트롤을 로드
        txtname = document.getElementById("name");
        age = document.getElementById("age");
        tel = document.getElementById("tel");
        tbody1 = document.getElementById("tbody1");
        
        
        
        //사용자의 입력을 편하게.. 이름 상자에 포커스
        //name.focus();
        
        //나이 추가
        for (var i=20; i<=60; i++) {
            //age.innerHTML += "<option value='" + i + "'>" + i + "세</option>";
            
            var op = document.createElement("option");
            op.setAttribute("value", i);
            op.innerText = i + "세";
            
            age.appendChild(op);
            
            age.value = 35;
        }
        
        
        
        //임의의 데이터 추가
        //addData();
        
        
        
        //테이블의 행에 이벤트 추가
        var tds = document.getElementsByTagName("td");
        //alert(tds.length);
        for (var i=0; i<tds.length; i++) {
            tds[i].onmouseover = rowFocus;
            
            tds[i].onmouseout = rowBlur;
        }            
    }
        
    function rowBlur() {
        event.srcElement.parentNode.style.backgroundColor = "transparent";//투명
    }
    
    function rowFocus() {
        //event.srcElement.style.backgroundColor = "yellow";
        event.srcElement.parentNode.style.backgroundColor = "gray";
    }
    
    function addData() {
        tbody1.innerHTML = "";//초기화
        
        for (var i=0; i<10; i++) {
            tbody1.innerHTML += "<tr><td>1</td><td>홍길동</td><td>20</td><td>010-5858-8585</td></tr>";
        }
    }        
    
    function add() {
        //이름, 나이, 연락처 입력 후 연락처 상자에서 엔터를 입력 -> 데이터 추가
        if (event.keyCode == 13) {
            
            //기존에 있는 <tr> 삭제(회원이 없습니다.)
            //alert(tbody1.firstChild.childNodes.length);
            
            if (tbody1.firstChild.childNodes.length == 3) {
                tbody1.removeChild(tbody1.firstChild);
            }
            
            
            //입력 데이터를 테이블의 행을 추가
            var tr = document.createElement("tr");
            var td1 = document.createElement("td");
            var td2 = document.createElement("td");
            var td3 = document.createElement("td");
            var td4 = document.createElement("td");
            var td5 = document.createElement("td");
            
            td1.innerText = no; //번호
            td2.innerText = txtname.value;
            td3.innerText = age.value;
            td4.innerText = tel.value;
            td5.innerHTML = "<span onclick='del();' style='cursor:pointer;'>[x]</span>";
            
            tr.appendChild(td1);
            tr.appendChild(td2);
            tr.appendChild(td3);
            tr.appendChild(td4); //조립(tr+td)
            tr.appendChild(td5);
            
            tbody1.appendChild(tr);
            
            no++;//번호 증가
            
            
            //입력 후 입력 양식을 초기화
            txtname.value = "";
            age.value = 35;
            tel.value = "";
            
            txtname.focus();
            
            
            //새롭게 추가된 행에도 이벤트 추가
            for (var i=0; i<tr.childNodes.length; i++) {
                tr.childNodes[i].onmouseover = rowFocus;
                tr.childNodes[i].onmouseout = rowBlur;
            }
            
        }        
    }
    
    function del() {
        //한행의 [x] 버튼을 눌렀을 때 호출
        //alert("삭제");
        //근데.. 누구를 삭제?
                
        if (confirm("정말 삭제하시겠습니까?")) {
            var tr = event.srcElement.parentNode.parentNode;
            tbody1.removeChild(tr);
        }
    }
    
</script>
 
</head>
<body onload="init();">
 
 
    <!-- ex19.htm -->
 
 
 
    <fieldset id="addForm">
        <legend>회원 정보 입력</legend>
        
        <label for="name">이름 : </label>
        <input type="text" id="name" /><br />
        
        <label for="age">나이 : </label>
        <select id="age"></select><br />
        
        <label for="tel">연락처 : </label>
        <input type="text" id="tel" onkeydown="add();" /><br />        
        
    </fieldset>
    
    <table id="tbl1">
        <thead>
            <tr>
                <th>번호</th>
                <th>이름</th>
                <th>나이</th>
                <th>연락처</th>
                <th>삭제</th>
            </tr>
        </thead>
        <tbody id="tbody1"><tr>
                <td colspan="5">현재 등록된 회원이 없습니다.</td>
            </tr></tbody>
    </table>
    
 
</body>
</html>
cs


'WEB > JS' 카테고리의 다른 글

문제] 주민번호 입력폼 구현하기  (0) 2016.01.22
form test  (0) 2016.01.22
js test  (0) 2016.01.22
타이머  (0) 2016.01.22
버튼 찾아 css 제거하기  (0) 2016.01.22

+ Recent posts