JavaScript Tips â dispatchEvent ã使ãããªãã!!
JavaScript ã® dispatchEvent ã®æ
å ±ããã¾ãã«ãå°ãªãã£ãã®ã§è§£èª¬ç¨ã«ãµã³ãã«ãä½ã£ã¦ã¾ã¨ãã¦ã¿ã¾ãã. ãã®ã¨ã³ããªã¼ã§ã¯ dispatchEvent
ã®åºæ¬çãªä½¿ãæ¹ã¨æ´»ç¨ä¾ã«ã¤ãã¦è§£èª¬ãã¾ã.
ãã¾ã使ãã°ãã¤ãã£ãã£ã½ã JavaScript ãæ¡å¼µãããã£ã¦ãã¨ãã§ãã便å©ãªé¢æ°ã§ã.
About
ä»åå¶ä½ãããµã³ãã«ãã¢ã¯ï¼ã¤.
æåã®ãµã³ãã«ã¯ã¤ãã³ããçºè¡ãããµã³ãã«ã§ã. boxB ãã¯ãªãã¯ããéã« boxA ã®ã¯ãªãã¯ã¤ãã³ããçºçããããã«ãªã£ã¦ãã¾ã.
2ã¤ç®ã®ãµã³ãã«ã¯ rightclick ã¤ãã³ã ã¨ãã å³ã¯ãªãã¯ããã¨ãã®ã¿çºè¡ããç¬èªã¤ãã³ããå®ç¾©ãã¦ãã¾ã.
Code
ä»åå¶ä½ãããµã³ãã«ãã¢ï¼åç®ã®å ¨ä½ã³ã¼ãã§ã.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
<script>
window.onload = function() {
var eBoxA = document.getElementById("boxA"); // boxA è¦ç´ åå¾
var eBoxB = document.getElementById("boxB"); // boxB è¦ç´ åå¾
// boxA ã«ã¯ãªãã¯ã¤ãã³ããç»é²
eBoxA.addEventListener("click", function(e){
log("boxA");
}, false);
// boxB ã«ã¯ãªãã¯ã¤ãã³ããç»é²
eBoxB.addEventListener("click", function(e){
log("boxB");
// eBoxA ã®ã¯ãªãã¯ã¤ãã³ãããã£ã¹ãããèµ·å
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
eBoxA.dispatchEvent( evt );
// webkit ç³»ã ã¨ä¸è¨ã§ã ok
//eBoxA.dispatchEvent(new Event("click"));
}, false);
};
var log = function(str) {
console.log(str);
var eConsole = document.getElementById("console");
eConsole.innerHTML += str;
eConsole.innerHTML += "\n";
};
</script>
</head>
<body>
<header>
<h1>JavaScript Tips - dispatchEvent ã使ãããªãã!! - ä»»æã®ã¿ã¤ãã³ã°ã§ã¤ãã³ããçºè¡ããã</h1>
<p>
boxB ãã¯ãªãã¯ããéã« boxA ã®ã¯ãªãã¯ã¤ãã³ããçºçããããã«ãã¦ã¿ã¾ãã.
</p>
<p>
boxA ãã¯ãªãã¯ããé㯠"boxA" ã¨ãã表示ããã¾ããã, boxB ãã¯ãªãã¯ãã㨠"boxB", "boxA" ã¨è¡¨ç¤ºãããã®ãããããã¨æãã¾ã.
</p>
</header>
<section>
<h2>main</h2>
<div id="main">
<div id="boxA" class="box">boxA</div>
<div id="boxB" class="box">boxB</div>
</div>
</section>
<section>
<h2>Console</h2>
<pre id="console"></pre>
<button onclick="document.getElementById('console').innerHTML = '';">Clear</button>
</section>
<footer>
<h2>Links</h2>
<a href="index.html">ããã</a>
</footer>
</body>
</html>
Tips
dispatchEvent ã¨ã¯
Flash 㧠AS ã触ã£ãçµé¨ã®ããæ¹ã«ã¨ã£ã¦ã¯ã馴æã¿ããããã¾ããã, dispatchEvent ã¨ã¯ååã®éãã¤ãã³ãããã£ã¹ããã(çºè¡)ããé¢æ°ã§ã. ããã使ãã°, A ãã¯ãªãã¯ããã¨ãã«ä¸ç·ã« B ã®ã¯ãªãã¯ã¤ãã³ããèµ·åããã, ç¬èªã®ã¤ãã³ããå®ç¾©ãããã£ã¦ãã¨ãå¯è½ã«ãªãã¾ã.
主ãªç¨éã¯ãã¡ã
- ä»»æã®ã¿ã¤ãã³ã°ã§ã¤ãã³ããçºè¡
- ç¬èªã¤ãã³ããå®ç¾©
dispatchEvent ã®ä½¿ãæ¹
dispatchEvent ã¯åå¾ãã dom è¦ç´ ã«å¯¾ãã¦å®è¡ãã¾ã. å¼æ°ã¯ã¤ãã³ããªãã¸ã§ã¯ãã§ã.
ä¾) ã¯ãªãã¯ã¤ãã³ãããã£ã¹ããã
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, elm);
elm.dispatchEvent( evt );
ãããå®è¡ãã㨠elm ãã¯ãªãã¯ãã¦ããªãã¦ãã¯ãªãã¯ã¤ãã³ããå®è¡ããã¾ã.
event ãªãã¸ã§ã¯ãã®åæåæ¹æ³
event ãªãã¸ã§ã¯ã㯠createEvent ã§çæããããªãã¸ã§ã¯ãåãæå®ã, çæãããªãã¸ã§ã¯ãã«å¿ããåæåã¡ã½ããã§åæåãã¾ã.
ä¸ã®ãµã³ãã«ã§ã¯å¼æ°ã 15åã渡ã㦠initMouseEvent ã¨ããã¡ã½ãããåæåã¡ã½ããã§ã. ãã®ä»æ§ããããã Flash ããã dispatchEvent ã®æ®åãé ããã¦ããè¦å ã®ä¸ã¤ã§ã¯ãªãããªã¨æãã¾ã.
ä»åã®ãµã³ãã«ã§ã¯ MouseEvent ãçæãã¦ãã¾ãã, ã»ãã«ã UIEvent ã ã¨ã KeyboardEvent ãªã©ããã, ããããããããã®å¼æ°ãåãåãåæåã¡ã½ãããããã¾ã.
å ¨ã¦è§£èª¬ãã¦ããã¨æ¥ãæ®ããã®ã§ä»å㯠MouseEvent ã®åæå initMouseEvent ã®å¼æ°ã«ã¤ãã¦ã ã解説ãã¾ã.
MouseEvent ã®å¼æ°åã¯ãããªæãã§ã.
event.initMouseEvent(type, canBubble, cancelable, view,
detail, screenX, screenY, clientX, clientY,
ctrlKey, altKey, shiftKey, metaKey,
button, relatedTarget);
type ... ã»ãããããã¦ã¹ã¤ãã³ãã®ã¿ã¤ãã§ã. è¨å®å¯è½ãªã¿ã¤ã㯠click, mosuedown, mouseup, mouseover, mousemove, mouseout ã§ã.
- canBubble ... ãããªã³ã°å¯è½ã. event.bubbles ã®å¤ãè¨å®ãã¾ã.
- cancelable ... ã¤ãã³ãã®ããã©ã«ãå¦çãå¶å¾¡å¯è½ã. event.cancelable ã®å¤ãè¨å®ãã¾ã.
- view ... ã¤ãã³ãã®AbstractView(æ½è±¡çãªãã¥ã¼?). ããæå³ããããã¾ããã, ã¨ãããã window ãè¨å®ãã¦ããã°åé¡ããã¾ãã.
- detail ... ãã¦ã¹ã¯ãªãã¯ã®åæ°
- screenX ... ã¤ãã³ãã®ç»é¢ã®X座æ¨. evnet.screenX ã®å¤ãè¨å®ãã¾ã.
- screenY ... ã¤ãã³ãã®ç»é¢ã®Y座æ¨. evnet.screenY ã®å¤ãè¨å®ãã¾ã.
- clientX ... ã¤ãã³ãã®ã¯ã©ã¤ã¢ã³ãã®X座æ¨. evnet.clientX ã®å¤ãè¨å®ãã¾ã.
- clientY ... ã¤ãã³ãã®ã¯ã©ã¤ã¢ã³ãã®Y座æ¨. evnet.clientY ã®å¤ãè¨å®ãã¾ã.
- ctrlKey ... ã¤ãã³ãä¸ã« Ctrl ãã¼ãæ¼ããç¶æ ã«ãããã©ãã. bool å¤.
- altKey ... ã¤ãã³ãä¸ã« Alt ãã¼ãæ¼ããç¶æ ã«ãããã©ãã. bool å¤.
- shiftKey ... ã¤ãã³ãä¸ã« Shift ãã¼ãæ¼ããç¶æ ã«ãããã©ãã. bool å¤.
- metaKey ... ã¤ãã³ãä¸ã« Meta ãã¼ãæ¼ããç¶æ ã«ãããã©ãã. bool å¤.
- button ... ãã¦ã¹ãã¿ã³ã®ç¶æ . event.button ã®å¤ãè¨å®ãã¾ã. é常ã®ãã¦ã¹ã¤ãã³ãã¨åæ§ 0:å·¦ã¯ãªãã¯, 1:ä¸å¤®ã¯ãªãã¯, 2:å³ã¯ãªãã¯ã§ã.
- relatedTarget ... ã¤ãã³ãã«é¢é£ããã¤ãã³ãã¿ã¼ã²ããã§ã. mouseover ã mouseout ã§ä½¿ç¨ãã¾ã. ä»ã®ã±ã¼ã¹ã§ã¯ null ã渡ãã¾ã.
ä»ã®ã¤ãã³ãã«ã¤ãã¦ã¯ãã¡ããåèã«ãã¦ãã ãã.
Reference
- Document.createEvent() - Web APIs | MDN
- MouseEvent.initMouseEvent() - Web APIs | MDN
- javascript event initMouseEvent( )
- ãJavaScriptã createEventã¨initMouseEventãè¦ãã ãã¡ã¢ã | mutter
- stmndrjfr - JavaScript ã§ã¯ãªãã¯ãã
次å㯠dispatchEvent ã使ã£ã¦ããã¹ãã¨ãªã¢ã®å¤ã®å¤æ´ããªã¢ã«ã¿ã¤ã ã§æ¤ç¥ããã¤ãã³ããå®ç¾©ãããã¨æãã¾ã.