teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2017/06/30 01:13

投稿

yambejp
yambejp

スコア117892

answer CHANGED
@@ -2,24 +2,26 @@
2
2
  > <section class="sections_a, sections_b">
3
3
 
4
4
  クラスの区切りは空白文字なので、sections_aの後ろのカンマが悪さをします。
5
-
6
-
7
- こんな感じでHTMLCOllectionで掴んで、prototypeで調整
5
+ その上で、こんな感じでHTMLCOllectionで掴んで、prototypeで調整
8
6
  ```javascript
9
7
  window.onload=function(){
10
- a.addEventListener('click',function(){console.log('sec_a');});
8
+ a.addEventListener('click',function(){console.log('sec_a');},true);
11
- b.addEventListener('click',function(){console.log('sec_b');});
9
+ b.addEventListener('click',function(){console.log('sec_b');},true);
12
10
  }
13
11
 
14
12
  var a= document.getElementsByClassName('sections_a');
15
13
  var b= document.getElementsByClassName('sections_b');
16
- HTMLCollection.prototype.addEventListener=function(x,y){
14
+ HTMLCollection.prototype.addEventListener=function(x,y,z){
15
+ if(typeof z=="undefined") z=false;
17
16
  Array.prototype.map.call(this,function(i){
18
- i.addEventListener(x,y);
17
+ i.addEventListener(x,y,z);
19
18
  });
20
19
  }
21
20
  ```
22
21
  ```HTML
23
22
  <div class="sections_a sections_b">test</div>
23
+ <div class="sections_a">test2</div>
24
+ <div class="sections_b">test3</div>
24
25
 
25
- ```
26
+ ```
27
+ (一部調整しました)