ã¡ã¢ãªã¼ãªã¼ã¯ãã¿ã¼ã³ãç解ãã
ã¡ã¢ãªã¼ãªã¼ã¯ã«é¢ããè¦ãæ¸ã
- ã¡ã¢ãªã¼ãªã¼ã¯ã¨ã¯
- ã³ã³ãã¥ã¼ã¿ã®åä½ä¸ã«ã使ç¨å¯è½ãªã¡ã¢ãªå®¹éããã ãã ãæ¸ã£ã¦ããç¾è±¡ã
- OSãã¢ããªã±ã¼ã·ã§ã³ã½ããããå¦çã®ããã«å æããã¡ã¢ãªé åãã解æ¾ããªãã¾ã¾æ¾ç½®ãã¦ãã¾ãããã«èµ·ããã
- ã¡ã¢ãªã¼ãªã¼ã¯ãã¿ã¼ã³ï¼ä»¥ä¸ã®ã¿ã¤ããèå¯ï¼
- ããã¥ã¡ã³ãããªã¼ã«å±ããªããã¼ãã«ãã¤ãã³ããè²¼ãä»ããå ´åã
- DOM ããã»ããµã管çãã¦ãããªãã¸ã§ã¯ãã¨ãã¹ã¯ãªããã¨ã³ã¸ã³ã管çãã¦ãããªãã¸ã§ã¯ãããæ··å¨ããå ´å
- ããããã¯ãã¼ã¸ã£ãããã®æ··å¨ãæããã¿ã¼ã³ï¼ãããé£è§£ï¼
- ã¡ã¢ãªã¼ãªã¼ã¯ã«è©²å½ããªããã®
- DOM ããã»ããµã管çãã¦ããå¤æ°ããã°ãã¼ãã«å¤æ°ã§ãã
- window, document ã¯ãã°ãã¼ãã«å¤æ°ãªã®ã§ã該å½ããªãã
ããã¥ã¡ã³ãããªã¼ã«å±ããªããã¼ãã«ãã¤ãã³ããè²¼ãä»ããå ´å
- ãã¼ã¸ãç ´æ£ããã¨ãã«ãããã¥ã¡ã³ãããªã¼ã®ãã¼ãããã©ã£ã¦ã¤ãã³ããåãé¢ãããããããã«å±ãã¦ããªãã¨ãåé¤ãããªãã
- ãªã¼ã¯ããã¡ã¢ãªã¯æ°ãã¤ãç¨åº¦ãªã®ã§ãå°éãªãç¡è¦ã§ãã
//createElement ã§ä½ã£ããã¼ãã«ãã¤ãã³ããè²¼ãä»ããã ãã®ã³ã¼ã function hoge ( ) { var e = document.createElement( 'div' ); e.onclick = function () { ; }; }
DOM ããã»ããµã管çãã¦ãããªãã¸ã§ã¯ãã¨ã
ãã¹ã¯ãªããã¨ã³ã¸ã³ã管çãã¦ãããªãã¸ã§ã¯ããéãå ´å
// ãã¼ãã®ãªãªã¸ãã«ãªããããã£ã«ããªãã¸ã§ã¯ãã追å ããã³ã¼ã var e = document.getElementById( 'hoge' ); e.fuga = function ( ) { ; };
- å¤æ° (e) ã®åç
§å
( hoge )ã¯ã DOM ããã»ããµã管çãã¦ãããªãã¸ã§ã¯ãã§ãããã«ä»ã足ãã .fuga ã¯ãã¹ã¯ãªããã¨ã³ã¸ã³ã管çãã¦ãããªãã¸ã§ã¯ãã§ããã
ããããã¡ã¢ãªã¼ãªã¼ã¯ãæããã¿ã¼ã³ã§ãã
ï¼å¤æ°ã«ä»£å
¥ããããã¼ãã«ãåæãªããããã£ãä»ãã¦ã¯ãªããªããï¼
- ä¸è¦ã循ç°ãã¦ããããã§ããã¹ã¯ãªããã¨ã³ã¸ã³ã管çãã¦ãããªãã¸ã§ã¯ããªã®ã§ããªã¼ã¯ããªãã
ï¼obj.fugaã«ã¯ãobjã®åç §ã¢ãã¬ã¹ãä¿åãããã ãã ãããããã¨åæãªæãè¾¼ã¿ï¼
// ã¹ã¯ãªããã¨ã³ã¸ã³ã管çãã¦ãããªãã¸ã§ã¯ãã循ç°ããã³ã¼ã var obj = new Object; obj.fuga = obj;
ããããã¯ãã¼ã¸ã£ãåé¡ã«ãªããã¿ã¼ã³
- ã¯ãã¼ã¸ã£ã«ãªãã³ã¼ãï¼
function hoge ( id ) { var e = document.getElementById( id ); e.onclick = function ( evt ) { alert( e.id ); // å¤æ° e ãä¿æ ã}; }
- ã¯ãã¼ã¸ã£ã«ãªãã³ã¼ãï¼(以å¾ãç解ããããã®ã§ãã¡ãã使ãï¼
function hoge ( id ) { var e = document.getElementById( id ); var f = function ( evt ) { alert( e.id ); }; e.onclick = f; }
ã©ã®ãããªãã¨ãããã£ã¦ãåé¡ã«ãªãã®ãï¼
- ã¾ãããããç解ããããããã«è²åãããããã¨ã«ããã
- DOM ããã»ããµã管çã管çãã¦ããé¨åãéè²
- ã¹ã¯ãªããã¨ã³ã¸ã³ã管çãã¦ããé¨åã赤è²
- ã°ãã¼ãã«å¤æ°ãç·è²
- ã¡ã¢ãªã¼ãªã¼ã¯ã¯ã循ç°åç
§ãã¦ãããªãã¸ã§ã¯ãã®ç®¡è½ãç°ãªãå ´åã«çºçããã
ã¤ã¾ããDOM ããã»ããµã管çãã¦ãããªãã¸ã§ã¯ããããã¹ã¯ãªããã¨ã³ã¸ã³ã管çãã¦ãããªãã¸ã§ã¯ãã«ãããã¦ã¾ã DOM ããã»ããµã管çãã¦ãããªãã¸ã§ã¯ãã«ã¨ã循ç°åç §ãæç«ããæã«ãªã¼ã¯ããã
ï¼ãªãã¸ã§ã¯ãã®ç®¡çè ãåããªãã循ç°åç §ãã¦ãåé¡ã¯ãªãï¼
(function outerFunc() { var e = document.getElementById( 'hoge' ); var innerFunc = function ( evt ) { alert( e.nodeName ); }; e.onclick = innerFunc; })();
-
- eï¼DOMï¼ â onclickï¼DOMï¼ â Functionï¼scriptï¼ â eï¼DOMï¼ã¨ã
管è½ãç°ãªããªãã¸ã§ã¯ãã®å¾ªç°åç §ãæç«ããã - è²ã§è¡¨ç¾ããã°ãé赤éã¨ãªã£ãã¨ãã«ã循ç°åç §ãããï¼ã¡ã¢ãªã¼ãªã¼ã¯ã¨ãªãï¼
- eï¼DOMï¼ â onclickï¼DOMï¼ â Functionï¼scriptï¼ â eï¼DOMï¼ã¨ã
- ä¸è¦ããã¨å¾ªç°åç §ãã¦ããªããããªã®ã§ãåéããã¦ãã¾ãå ´å
(function outerFunc() { var e = document.getElementById( 'hoge' ); var innerFunc = function ( evt ) { var element = evt.target; alert( element.nodeName ); }; e.onclick = innerFunc; })();
-
- é¢æ° (outerFunc) ã®å¤æ° (e) ã¯ã onclick ã®ã¤ãã³ããçºçããã¨ãé¢æ° (innerFunc) ãå®è¡ãããããã«ãªã£ã¦ãããããããããã«ã¯ãå¤å´ã§ä½¿ããã¦ããå¤æ° (e)ã使ããã¦ããªãã
ãã ãã循ç°åç §ã«ãªã£ã¦ããªããã¨èããã®ã¯ãééãã§ããã
- é¢æ° (outerFunc) ã®å¤æ° (e) ã¯ã onclick ã®ã¤ãã³ããçºçããã¨ãé¢æ° (innerFunc) ãå®è¡ãããããã«ãªã£ã¦ãããããããããã«ã¯ãå¤å´ã§ä½¿ããã¦ããå¤æ° (e)ã使ããã¦ããªãã
-
- é¢æ° (innerFunc) ã® element 㨠é¢æ° (outerFunc) ã®å¤æ° (e)ã¯ãåããã¼ã(hoge)ãåç §ãã¦ããããªã®ã§ããã循ç°åç §ã§ãªã¼ã¯ãã¦ããèããã®ãééãã§ããã
-
- ããããã¯ãã¼ã¸ã£ãçæããæç¹ã§ãé¢æ° (innerFunc) ã¯ãå¤æ° (e) ãããã¤ã§ãåç §ã§ããããã«ãªã£ã¦ããããªã®ã§å¾ªç°åç §ãæç«ããã
-
- è²ã§è¡¨ç¾ããã¨ã
e(DOM) â onclick(DOM) â innerFunc(script) â e(DOM) ãç¡ãã¦ãåç §å¯è½ã«ãªã£ã¦ããï¼
- è²ã§è¡¨ç¾ããã¨ã
- ã¡ã¢ãªã¼ãªã¼ã¯ãæã¡åã£ã¦ãã¾ã
(function outerFunc() { var e = document.getElementById( 'hoge' ); var innerFunc = function ( evt ) { alert( e.nodeName ); }; e.onclick = innerFunc; e = null; // ãããã¹ã¯ãªããã管çãã¦ãããªãã¸ã§ã¯ãã«ãã¦ãã¾ã })();
-
- ãã解説ã¯ããããªãã ããã
- 以ä¸ã¯ããªã¼ã¯ããªã
(function outerFunc() { var e = 'hoge'; var innerFunc = function ( evt ) { var element = document.getElementById( e ); alert( element.nodeName ); }; document.getElementById( e ).onclick = innerFunc; })();
-
- document.getElementById( e ) ã®ãonclick ã§å¼ã°ãããé¢æ° (innerFunc) ããåç §ã§ããå¤æ° (e) ã¯ãã¹ã¯ãªããã¨ã³ã¸ã³ã管çãã¦ãããªãã¸ã§ã¯ãã§ããã
-
- è²ã§è¡¨ç¾ããã¨ã
document.getElementById( e ) â onclick(DOM) â innerFunc(script) â e(DOM) ãç¡ãã¦ãåç
§ã§ããï¼
- ä¸è¦ããã¨å¾ªç°åç §ãã¦ããªããããªã®ã§ãåéããã¦ãã¾ãå ´å ãã®ï¼
(function outerFunc() { var e = 'hoge'; var d = document; //âããã追å var innerFunc = function ( evt ) { var element = document.getElementById( e ); alert( element.nodeName ); }; document.getElementById( e ).onclick = innerFunc; })();
-
- document.getElementById( e ) ã®ãonclick ã§å¼ã°ãããé¢æ° (innerFunc) ããåç §ã§ããå¤æ° (d) ã¯ãã°ãã¼ãã«å¤æ°ã® document ããä¿åããã¦ããããå¤æ° (d) èªä½ã¯ããã¼ã«ã«å¤æ°ãªã®ã§ãåç §ãããã¨ãã§ããããªã®ã§ã¡ã¢ãªã¼ãªã¼ã¯ããã
-
- è²ã§è¡¨ç¾ããã¨ã document.getElementById( e ) â onclick(DOM) â innerFunc(script) â d(DOM)
- ä¸è¨ã¯ã¡ã¢ãªãªã¼ã¯ãã¿ã¼ã³ã«è©²å½ããªã
function hoge() { ãdocument.getElementById('hoge').onclick = function(e) { alert( 'abc' ); ã}; }
-
- å¤å´ã«åç §å¯è½ãªãDOM ããã»ããµã管çããã¦ãããã¼ã«ã«å¤æ°ããªã
- ä¸è¨ãåæ§ã
function hoge() { ã var nodes = document.getElementsByTagName('A'); ãvar I = nodes.length; ãvar i; ãfor (i = 0; i < I; i++) { ãã nodes[i].onclick = function() { ããã// å¤æ° nodesãIãi ãä¿æ ãã}; ã} }
-
- ã¾ã ã¾ã ç¶ãäºå®
çµå±ããããããã®ã ã
- ããã¯ã¾ã ãæ¸ãããã§ã
- ä¸ã¯ä½ããããã®ãã¨ããã¨ãã¤ãã³ããè²¼ãä»ãã node ã®è¦ªããã®itemçªå·ã§ãã®nodeã
è¦ã¤ãã¦ããã®çãããªã¹ãã«è²¼ãä»ããããããªãã¡ã¢ãªã¼ãªã¼ã¯ããªãï¼ï¼
- ããããã®çå符ãã¨ããã®ã¯ããã¤ã®ãã¨ããã»ã»ã»
function addEvent ( element, evt, eventHandler, flag ) { var cnt = 0, n = element, t; if( element != document ) { while( n = n.previousSibling ) cnt++; t = element.parentNode; t.item( cnt )./*@if( @_jscript ) attachEvent( 'on' + @else@*/ addEventListener( /*@end@*/ evt, eventHandler, flag ); } }