回答編集履歴

1

handleEventのコード追記

2016/10/29 05:42

投稿

think49
think49

スコア18164

test CHANGED
@@ -1,6 +1,8 @@
1
1
  複数の機能間でデータを共有する方法は handleEvent, data-*属性、カスタムイベント、WeakMap 等があります。
2
2
 
3
+
4
+
3
- ご掲示のコードでは私なら `handleEvent` を使いますね。
5
+ ### handleEvent
4
6
 
5
7
 
6
8
 
@@ -8,17 +10,17 @@
8
10
 
9
11
  <div class="demo">
10
12
 
11
- <ul>
13
+ <ul>
12
14
 
13
- <li id="list1">list1</li>
15
+ <li id="list1">list1</li>
14
16
 
15
- <li id="list2">list2</li>
17
+ <li id="list2">list2</li>
16
18
 
17
- <li id="list3">list3</li>
19
+ <li id="list3">list3</li>
18
20
 
19
- </ul>
21
+ </ul>
20
22
 
21
- <button type="button" id="button">Button</button>
23
+ <button type="button" id="button">Button</button>
22
24
 
23
25
  </div>
24
26
 
@@ -26,15 +28,25 @@
26
28
 
27
29
  'use strict';
28
30
 
29
- document.querySelector('demo').addEventListener('click', {
31
+ document.querySelector('.demo').addEventListener('click', {
30
32
 
31
33
  text: '',
32
34
 
33
- function handleClick (event) {
35
+ handleEvent: function handleClick (event) {
34
36
 
35
37
  var target = event.target;
36
38
 
39
+
40
+
41
+ if (target.tagName === 'LI') {
42
+
43
+ this.text = target.firstChild.data;
44
+
45
+ } else if (target.id === 'button') {
46
+
37
- // ほげほげ(後で書きます)
47
+ console.log(this.text);
48
+
49
+ }
38
50
 
39
51
  }
40
52
 
@@ -46,4 +58,28 @@
46
58
 
47
59
 
48
60
 
61
+ ### data-*属性
62
+
63
+
64
+
65
+ 要追記。
66
+
67
+
68
+
69
+ ### WeakMap
70
+
71
+
72
+
73
+ 要追記。
74
+
75
+
76
+
77
+ ### カスタムイベント
78
+
79
+
80
+
81
+ 要追記。
82
+
83
+
84
+
49
85
  Re: kerokero335 さん