回答編集履歴

4

+

2018/05/12 02:48

投稿

think49
think49

スコア18164

test CHANGED
@@ -174,7 +174,7 @@
174
174
 
175
175
  function handleClick (event) {
176
176
 
177
- const index = event.target.getAttribute('data-index');
177
+ const index = +event.target.getAttribute('data-index');
178
178
 
179
179
 
180
180
 

3

Bubbling Phase

2018/05/12 02:48

投稿

think49
think49

スコア18164

test CHANGED
@@ -76,4 +76,124 @@
76
76
 
77
77
 
78
78
 
79
+ ### data-*属性
80
+
81
+
82
+
83
+ ```HTML
84
+
85
+ <button class="btn" data-index="1">1</button>
86
+
87
+ <button class="btn" data-index="2">2</button>
88
+
89
+ <button class="btn" data-index="3">3</button>
90
+
91
+ <button class="btn" data-index="4">4</button>
92
+
93
+ <button class="btn" data-index="5">5</button>
94
+
95
+ <button class="btn" data-index="6">6</button>
96
+
97
+ <button class="btn" data-index="7">7</button>
98
+
99
+ <button class="btn" data-index="8">8</button>
100
+
101
+ <button class="btn" data-index="9">9</button>
102
+
103
+ <script>
104
+
105
+ 'use strict';
106
+
107
+ function handleClick (event) {
108
+
109
+ const index = event.currentTarget.getAttribute('data-index');
110
+
111
+
112
+
113
+ if (/[1-5]/.test(index)) {
114
+
115
+ console.log('btn' + index);
116
+
117
+ }
118
+
119
+ }
120
+
121
+
122
+
123
+ for (let btn of document.querySelectorAll('.btn')) {
124
+
125
+ btn.addEventListener('click', handleClick, false);
126
+
127
+ }
128
+
129
+ </script>
130
+
131
+ ```
132
+
133
+
134
+
135
+ ### Bubbling Phase
136
+
137
+
138
+
139
+ 目的次第ですが、私は基本的にこの方法を使います。
140
+
141
+
142
+
143
+ - [3.1. イベント配送と DOM イベントフロー - UI Events (日本語訳)](https://triple-underscore.github.io/uievents-ja.html#event-flow)
144
+
145
+
146
+
147
+ ```HTML
148
+
149
+ <p id="btn-group">
150
+
151
+ <button class="btn" data-index="1">1</button>
152
+
153
+ <button class="btn" data-index="2">2</button>
154
+
155
+ <button class="btn" data-index="3">3</button>
156
+
157
+ <button class="btn" data-index="4">4</button>
158
+
159
+ <button class="btn" data-index="5">5</button>
160
+
161
+ <button class="btn" data-index="6">6</button>
162
+
163
+ <button class="btn" data-index="7">7</button>
164
+
165
+ <button class="btn" data-index="8">8</button>
166
+
167
+ <button class="btn" data-index="9">9</button>
168
+
169
+ </p>
170
+
171
+ <script>
172
+
173
+ 'use strict';
174
+
175
+ function handleClick (event) {
176
+
177
+ const index = event.target.getAttribute('data-index');
178
+
179
+
180
+
181
+ if (index < 6) {
182
+
183
+ console.log('btn' + index);
184
+
185
+ }
186
+
187
+ }
188
+
189
+
190
+
191
+ document.getElementById('btn-group').addEventListener('click', handleClick, false);
192
+
193
+ </script>
194
+
195
+ ```
196
+
197
+
198
+
79
199
  Re: Tubuanpan さん

2

typo修正

2018/05/12 02:44

投稿

think49
think49

スコア18164

test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
  case 'btn5':
38
38
 
39
- consle.log(token);
39
+ console.log(token);
40
40
 
41
41
  break;
42
42
 
@@ -62,7 +62,7 @@
62
62
 
63
63
  if (/^btn\d+$/.test(token)) {
64
64
 
65
- consle.log(token);
65
+ console.log(token);
66
66
 
67
67
  break;
68
68
 

1

addEventListener

2018/05/12 02:05

投稿

think49
think49

スコア18164

test CHANGED
@@ -1,3 +1,19 @@
1
+ ### addEventListener
2
+
3
+
4
+
5
+ > ```JavaScript
6
+
7
+ > btn[i].addEventListener('click', btnClick);
8
+
9
+ > ```
10
+
11
+
12
+
13
+ 現在の `addEventListener` は第三引数を省略可能ですが、後方互換性の為に、第三引数は省略しない事をお勧めします。
14
+
15
+
16
+
1
17
  ### switch 文
2
18
 
3
19