ãã£ããã£ãªã³ã°ã¨ãããªã³ã° Firefoxç·¨
ãµã¤æ¬ã®17.2ã«è©³ããæ¸ãã¦ããã¾ãããJavaScriptã®DOM ã¬ãã«2ã¤ãã³ãã§ã¯ã
ã¤ãã³ãä¼æã¯3ã¤ã®æ®µéã§æ§æããã¾ãã
- ãã£ããã£ãªã³ã°ãã§ã¼ãº
- ã¿ã¼ã²ãããã¼ãèªèº«ã§ã®ã¤ãã³ããã³ãã©ã®å®è¡
- ãããªã³ã°ãã§ã¼ãº
ä¸å³ã®ããã«3ã¤å
¥ãåã«ãªã£ãè¦ç´ ã®ã
å¤å´ãã2ã¤ç®ã®è¦ç´ ãã¯ãªãã¯ããå ´åãä¾ã«ã¨ãã¨ã
ãã£ããã£ãªã³ã°ãã§ã¼ãºã§documentâå¤å´ã®è¦ç´ âã¨ä¼æãã¦ããã
ã¿ã¼ã²ãããã¼ãã§ã®ã¤ãã³ããã³ãã©(ããã°)ãå®è¡ããã
ãããªã³ã°ãã§ã¼ãºã§âå¤å´ã®è¦ç´ âdocumentã¨ä¼æãã¦ããã¾ãã
DOMã¬ãã«0ã®ã¤ãã³ãã§ã¯ã¿ã¼ã²ãããã¼ãã§ãã
ã¤ãã³ããã³ãã©ã®å®è¡ã¯åºæ¥ã¾ãããã
DOMã¬ãã«2ã¤ãã³ãã§ã¯ãã£ããã£ãªã³ã°ãã§ã¼ãºã»ãããªã³ã°ãã§ã¼ãºã§ã
ã¤ãã³ããã³ãã©ã®å®è¡ãåºæ¥ã¾ãã
æ¤è¨¼ç¨HTML
確èªã®ããã«divã®è¦ªåãããããã¯ãªãã¯ãã¦ã
ã¤ãã³ãã®logãåãããã ãã®ç°¡åãªHTML+JavaScriptãç¨æãã¾ããã
ãã®HTMLãhtmlãã¡ã¤ã«ã«ãã¦éãã¨ãã¯ãå¿
ãFirefoxã§éãã¦ãã ããã
ã§ãªãã¨ã説æã©ããã®åä½ã¯ãã¾ããã
<html> <head> <script> (function(func){ var temp = []; log = function(s){ temp.push(s); }; function init(){ if(typeof document.readyState != 'undefined' && document.readyState != 'loaded' && document.readyState != 'complete'){ setTimeout(arguments.callee,100); return; } var div = document.createElement('div'); div.id = 'log'; for(var i = 0;i < temp.length;i++){ div.appendChild( document.createTextNode(temp[i]) ); div.appendChild( document.createElement('br') ); } document.body.appendChild(div); log = function(s){ div.appendChild( document.createTextNode(s) ); div.appendChild( document.createElement('br') ); }; func(); } if(typeof document.readyState == 'undefined' && document.addEventListener){ document.addEventListener('DOMContentLoaded',init, false ); }else{ init(); } })(function(){ /* ãã¼ãæã«å®è¡ãããã³ã¼ã */ document.getElementById('outer1').addEventListener('click',function(e){eventLog(e);},true); document.getElementById('inner1').addEventListener('click',function(e){eventLog(e);},true); document.getElementById('outer2').addEventListener('click',function(e){eventLog(e);},false); document.getElementById('inner2').addEventListener('click',function(e){eventLog(e);},false); document.getElementById('outer3').addEventListener('click',function(e){eventLog(e);},true); document.getElementById('inner3').addEventListener('click',function(e){eventLog(e);},true); document.getElementById('outer3').addEventListener('click',function(e){eventLog(e);},false); document.getElementById('inner3').addEventListener('click',function(e){eventLog(e);},false); document.getElementById('outer4').addEventListener('click',function(e){eventLog(e);e.stopPropagation();},true); document.getElementById('inner4').addEventListener('click',function(e){eventLog(e);},true); document.getElementById('outer5').addEventListener('click',function(e){eventLog(e);},false); document.getElementById('inner5').addEventListener('click',function(e){eventLog(e);e.stopPropagation();},false); document.getElementById('hoge').addEventListener('click',function(e){eventLog(e);},false); document.getElementById('outer6').addEventListener('click',function(e){eventLog(e);e.stopPropagation();},false); document.getElementById('inner6').addEventListener('click',function(e){eventLog(e);},false); }); function eventLog(e){ log([ ['eventPhase',e.eventPhase].join(':'), ['target',e.target['id']].join(':'), ['currentTarget',e.currentTarget['id']].join(':') ].join()); } </script> <style> .outer{ border:solid; width:40px; height:40px; } .inner{ border:solid; width:20px; height:20px; } </style> </head> <body> (1)å å¤å ±ã«useCaptureã«trueãè¨å® <div id="outer1" class="outer"> <div id="inner1" class="inner"> </div> </div> (2)å å¤å ±ã«useCaptureã«falseãè¨å® <div id="outer2" class="outer"> <div id="inner2" class="inner"> </div> </div> (3)å å¤å ±ã«ãuseCaptureã«trueã»false両æ¹ã§ã¤ãã³ããã³ãã©ãè¨å® <div id="outer3" class="outer"> <div id="inner3" class="inner"> </div> </div> (4)å å¤å ±ã«useCaptureã«trueãè¨å®ãå¤å´ã§Event.stopPropagation()ãå®è¡ãã <div id="outer4" class="outer"> <div id="inner4" class="inner"> </div> </div> (5)å å¤å ±ã«useCaptureã«falseãè¨å®ãå å´ã§Event.stopPropagation()ãå®è¡ãã <div id="outer5" class="outer"> <div id="inner5" class="inner"> </div> </div> (6)3ã¤ãã¹ã¦ã§useCaptureã«falseãè¨å®ãå¤å´ãã2çªç®ã§Event.stopPropagation()ãå®è¡ãã <div id="hoge" style="border:solid;width:80px;height:80px;"> <div id="outer6" class="outer"> <div id="inner6" class="inner"> </div> </div> </div> </body> </html>
ãã°ã®è¦æ¹ã¯ä»¥ä¸ã®éãã§ãã
eventPhase:${ã¤ãã³ããã§ã¼ãº},target:${ã¿ã¼ã²ãããã¼ãã®id},currentTarget:${ç¾å¨ã¤ãã³ããå¦çããã¦ãããã¼ãã®id} 注:ã¤ãã³ããã§ã¼ãºã¯ã1ã»ãã£ããã£ãªã³ã°ã2ã»ã¿ã¼ã²ãããã¼ãã3ã»ãããªã³ã°
ã¤ãã³ããã³ãã©è¨å®ç¨ã®ã¡ã½ããã¯ä»¥ä¸ã®ããã«ãªã£ã¦ãã¦ã
useCaptureã«trueãè¨å®ããã¨ãã£ããã£ãªã³ã°ãã§ã¼ãºã«ã¤ãã³ããã³ãã©ãå®è¡ã
falseãè¨å®ããã¨ãããªã³ã°ãã§ã¼ãºã«ã¤ãã³ããã³ãã©ãå®è¡ããã¾ãã
addEventListener(type,listener,useCapture)
(1)å å¤å ±ã«useCaptureã«trueãè¨å®
ãã®å ´åã¿ã¼ã²ãããã¼ãã®å ´åã¨ãã£ããã£ãªã³ã°ãã§ã¼ãºã«ã¤ãã³ããã³ãã©ãå®è¡ããã¾ãã
å¤å´ã®div(id=outer1)ãã¯ãªãã¯ããã¨ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:outer1,currentTarget:outer1
å å´ã®div(id=inner1)ãã¯ãªãã¯ããå ´åã¯ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:1,target:inner1,currentTarget:outer1 eventPhase:2,target:inner1,currentTarget:inner1
å¤å´ãå
å´ã¨ããé ã«ã¤ãã³ããã³ãã©ãå®è¡ããã¦ãããã¨ã
å¤å´ã®divã«è¨å®ããã¤ãã³ããã³ãã©å®è¡æã¯ã
eventPhase:1ï¼ãã£ããã£ãªã³ã°ãã§ã¼ãºã§ãããã¨ãåããã¾ãã
(2)å å¤å ±ã«useCaptureã«falseãè¨å®
ãã®å ´åå ´åã¿ã¼ã²ãããã¼ãã®å ´åã¨ãããªã³ã°ãã§ã¼ãºã«ã¤ãã³ããã³ãã©ãå®è¡ããã¾ãã
å¤å´ã®div(id=outer2)ãã¯ãªãã¯ããã¨ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:outer2,currentTarget:outer2
å å´ã®div(id=inner2)ãã¯ãªãã¯ããå ´åã¯ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:inner2,currentTarget:inner2 eventPhase:3,target:inner2,currentTarget:outer2
å
å´ãå¤å´ã¨ããé ã«ã¤ãã³ããã³ãã©ãå®è¡ããã¦ãããã¨ã
å¤å´ã®divã«è¨å®ããã¤ãã³ããã³ãã©å®è¡æã¯ã
eventPhase:3ï¼ãããªã³ã°ãã§ã¼ãºã§ãããã¨ãåããã¾ãã
(3)å å¤å ±ã«ãuseCaptureã«trueã»false両æ¹ã§ã¤ãã³ããã³ãã©ãè¨å®
å¤å´ã®div(id=outer3)ãã¯ãªãã¯ããã¨ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:outer3,currentTarget:outer3 eventPhase:2,target:outer3,currentTarget:outer3
å å´ã®div(id=inner3)ãã¯ãªãã¯ããå ´åã¯ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:1,target:inner3,currentTarget:outer3 eventPhase:2,target:inner3,currentTarget:inner3 eventPhase:2,target:inner3,currentTarget:inner3 eventPhase:3,target:inner3,currentTarget:outer3
åè¿°ã®å³ã®ããã«å¤âå âå¤ã¨ããããã«ã¤ãã³ããä¼æãã¦ãããã¨ãåããã¾ãã
Event.stopPropagation()
ã¤ãã³ããã³ãã©ã®å¼æ°ã§ããEventãªãã¸ã§ã¯ãã®stopPropagationã¡ã½ãããå®è¡ããã¨ã
ãã以éã®ã¤ãã³ãã®ä¼æãåæ¢ããããã¾ãã
ã©ã®ãã§ã¼ãºã§ãã£ã¦ããã®ã¤ãã³ããã³ãã©ä»¥éã®ä¼æã¯åæ¢ãã¾ãã
(4)å å¤å ±ã«useCaptureã«trueãè¨å®ãå¤å´ã§Event.stopPropagation()ãå®è¡ãã
å¤å´ã®div(id=outer4)ãã¯ãªãã¯ããã¨ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:outer4,currentTarget:outer4
å å´ã®div(id=inner4)ãã¯ãªãã¯ããå ´åã¯ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:1,target:inner4,currentTarget:outer4
å
å´ã®divãã¯ãªãã¯ããå ´åãå¤å´ã®divã«è¨å®ãããã¤ãã³ããã³ãã©ã
ãã£ããã£ãªã³ã°ãã§ã¼ãºã§å®è¡ãããããã§ã¤ãã³ãã®ä¼æãæ¢ã¾ã£ã¦ããã®ãåããã¾ãã
((1)ã¨è¦æ¯è¼)
(5)å å¤å ±ã«useCaptureã«falseãè¨å®ãå å´ã§Event.stopPropagation()ãå®è¡ãã
å¤å´ã®div(id=outer5)ãã¯ãªãã¯ããã¨ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:outer5,currentTarget:outer5
å å´ã®div(id=inner5)ãã¯ãªãã¯ããå ´åã¯ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:inner5,currentTarget:inner5
å
å´ã®divãã¯ãªãã¯ããå ´åãã¿ã¼ã²ãããã¼ãã§ã¤ãã³ããã³ãã©ãå®è¡ããã
ã¤ãã³ãä¼æãæ¢ã¾ããå¤å´ã®divã®ã¤ãã³ããã³ãã©ãå®è¡ããã¾ããã
((2)ã¨è¦æ¯è¼)
(6)3ã¤å ¥ãåã®divãã¹ã¦ã§useCaptureã«falseãè¨å®ãå¤å´ãã2çªç®ã§Event.stopPropagation()ãå®è¡ãã
ä¸çªå¤å´ã®div(id=hoge)ãã¯ãªãã¯ããã¨ã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:hoge,currentTarget:hoge
éã®div(id=outer6)ãå
å´ã®div(id=inner6)ãã¯ãªãã¯ããå ´åã¯ã
ãããã以ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
eventPhase:2,target:outer6,currentTarget:outer6
eventPhase:2,target:inner6,currentTarget:inner6 eventPhase:3,target:inner6,currentTarget:outer6
å
å´ã®divããéã®divã¾ã§ã¯ãããªã³ã°ãã¦ã¾ããã
ããã§ã¤ãã³ãä¼æã¯æ¢ã¾ããä¸çªå¤å´ã®ã¤ãã³ããã³ãã©ã¯å®è¡ããã¾ããã