다중 이벤트와 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
<!DOCTYPE html>
<head>
<title>Document</title>
<style>
table {border-collapse: collapse; border-radius: 4px;}
thead th, tbody td{
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<thead>
<th>번호</th>
<th>이름</th>
<th>내용</th>
<th>삭제</th>
</thead>
<tbody class="list">
<tr>
<td>1</td>
<td>츄</td>
<td>안녕</td>
<td><button type="button">삭제</button></td>
</tr>
<tr>
<td>2</td>
<td>치치</td>
<td>안녕</td>
<td><button type="button">삭제</button></td>
</tr>
<tr>
<td>3</td>
<td>룰루</td>
<td>안녕</td>
<td><button type="button">삭제</button></td>
</tr>
<tr>
<td>4</td>
<td>말리</td>
<td>안녕</td>
<td><button type="button">삭제</button></td>
</tr>
<tr>
<td>5</td>
<td>츄</td>
<td>안녕</td>
<td><button type="button">삭제</button></td>
</tr>
</tbody>
</table>
<script>
var tbody = document.querySelector(".list");
var list = tbody.querySelectorAll("td button");
for(var i=0; i < list.length; i++){
list[i].onclick = function(){
/*
console.log(event); //이벤트 객체를 호출
console.log(arguments);
console.log(arguments[0]); // event === arguments
*/
//console.dir(event)
console.log(event.target) //event.target -> 마우스의 클릭 이벤트에 해당하는 노드
event.target.parentElement.parentElement.remove(); //button 노드의 부모 부모 노드인 tr 태그를 삭제
}
}
</script>
</body>
</html>