mixiããã¦ããããæ®éã®OLããä½æ NodeListã¨ãããã£ã¼ã«ãã«èãéããã®ãï¼
ã¨ã³ããªã®ã¿ã¤ãã«ãèããã®ãé¢åã«ãªã£ãã®ã§ããããã³ããªã¡ã¼ã«ã¼ã§ä½æãããã¯ãããã³ããªã¡ã¼ã«ã¼ã§ä½æããããã®ãããªã¿ã¤ãã«ãã¤ãããã¨ã«ãã¾ããã
getElementHogeHogeãè¿ãã®ã¯liveãªNodeListã§ãquerySelectorHogeHogeãè¿ãã®ã¯staticãªNodeListã ãã¨ããã®ã¯æ¢ã«ããç¥ããã¦ããã
ï¼åè: http://d.hatena.ne.jp/amachang/20080306/1204787459, http://web.g.hatena.ne.jp/vantguarde/?of=90ï¼
ä¼ç¤¾ã®äººã«ãã®ãã¨ã話ãã¦ãããããå é¨çãªã¢ã¬ãããããéãã®ã¯ããã¨ãã¦ãArray.prototype.slice.callã¨ãã§Arrayã«å¤æããå ´åã¯ã©ããªãã®ï¼ãã¨èããã¦ãã¦ã¼ã³ã»ã»ã»ãã¨ãªã£ã¦çããããªãã£ãã®ã§ãä»ãã30ç§ã§æ¤è¨¼ã³ã¼ããæ¸ãã¾ãã
...
ã¯ããæ¢ã«ä½ã£ã¦ãã£ãã³ã¼ãããã¡ãã«ãªãã¾ãã
(gist) https://gist.github.com/1924408
(jsdo.it) http://jsdo.it/zentooo/lqUD/fullscreen
ã¡ãªã¿ã«console.logã§çµæãåºãã¦ããã®ã§ãconsole.logã¨ããåºãç°å¢ãããªãã¨ä½ãåºãªãã§ãã
ã³ã¼ãã®æå³ã¨ãã¦ã¯ããDOMContentLoadedã®æç¹ã§NodeList, Arrayã«å¤æããNodeList, (Static)NodeList, Arrayã«å¤æãã(Static)NodeListãç¨æãã¦ãããããããè¦ç´ ã¯ãªãã¯ã§è²ã DOMã«å¤æ´å ãããã¨ä¸ã®ãã¿ã³ã§ãã³ããã¨ããããªæãã§ããä½ã ãåããã«ããã§ããããã¿ã³ã¨ãä¸ã®aã¨ãbã¨ãã®è¦ç´ ãããããã¯ãªãã³ãã¦ã¿ãã°å¤§ä½åãããã¨ã
ã¡ãªã¿ã«ãåElementãã¯ãªãã¯ããã¨çºåããDOMã®å¤æ´(åã°ã«ã¼ãã«ã¤ãã¦ä¸ãã 1.èªèº«ãDOM Treeããåé¤ã2.èªèº«ã®ã¯ãã¼ã³ã親è¦ç´ ã«è¿½å ã3.èªèº«ã®ã¯ãã¼ã³ãèªèº«ã«è¿½å )ããã©ã®ããã«ãã³ãçµæã«å½±é¿ãä¸ãããäºåã«å ¨é¨åãã£ã人ã¯ããã§ããããï¼åã¯ãªãã¨ãªãã®äºæ³ã¯ãã¦ãã¾ããããçµæ§ãºã¬ã¦ã¾ããã
ããããInterface NodeListã¯ãDOM 3 Coreã§ã¯
http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-536297177
ãããªæãã§ãããã§è¨ã£ã¦ã"live"ã¨ã¯ãªããããã¨ãªã³ã¯ã辿ãã¨
http://www.w3.org/TR/DOM-Level-3-Core/core.html#td-live
ãããªãã¨ãæ¸ãã¦ããããããªããããElementã®åè¦ç´ ããã¤NodeListãã²ããããã¨ãããããã®Elementã«æ°ããªè¦ç´ ã追å ããããã¾ãã¯è¦ç´ ãããããåé¤ããããè¦ç´ ãå¤æ´ããå ´åNodeListã«ãããã®å¤æ´ã¯èªåçã«åæ ããããåãããã«ãDOM Treeå ã®Nodeã«å¯¾ããå ¨ã¦ã®å¤æ´ã¯ãNodeListå ã®Nodeã«åæ ãããã
â¦ãåãããé ããã ãããï¼
ãããããèªä½ã®æå³ã¯åããã®ã ãã©ã
http://www.w3.org/TR/selectors-api/
ä¸è¨ãªã³ã¯å ã®ããã«ããstaticãªNodeListã£ã¦ã®ã¯è¦ã¯liveãããªãNodeListã ããããã¨æ¸ãããã¨ä½ã ãããããããªããªã£ã¦ãã¾ããå®éã®ããã¹ãæåã¯å®ã¯ãã®å¾ã«ããç¨åº¦ã¡ããã¨æ¸ãã¦ãããã ãã©ã"static = not live = liveã®æºããæ¡ä»¶ã¯å ¨é¨æºãããªã" ã£ã¦è§£éã ã¨ããstaticãªNodeListã§ã¯DOMã¸ã®å¤æ´ãNodeListã«åæ ãããªãã°ããããNodeListå ã®åElementã«ãåæ ãããªãã®ã¼ï¼ï¼ã¦ãã½ã¼ã³ï¼ãã¨ãªã£ã¦ãã¾ãã
ãã¡ããå®éã«ã¯ãããªãã¨ã¯ãªãã¦ãDOMãå¤æ´ãã(ä¾ãã°ãå½è©²Elementã«ä½ããappendChildãã)å ´åã«ã¯ãã®å¤æ´ã¯NodeListãliveã ãããstaticã ãããNodeListã®ä¸èº«ã®Elementã«åæ ããããã¨ããããããããªãã¨ããã°ã©ã ãæ¸ãã«ããã¦ãããããªãã
è¦ã¯ããDOMã®å¤æ´ãNodeListã«åæ ããããå¦ã â DOMã®å¤æ´ãNodeListå ã®Elementã«åæ ããããå¦ããã ã¨ãããã¨ãã¡ãã£ã¨èãã¦ã¿ãã°åãããã¨ã ãã©ãç´ããããã
ãªãã±ã¤ãã§ã«çãåãããããã¨ã
1) document.getElementsByClassName("a")ã§å¾ãliveãªNodeListã¯ãdocumentã«class=aãªElementã追å ãããã¨è¿½éãã¦è¦ç´ ãä¸ã¤å¢ããã
2) document.getElementsByClassName("b")ã§å¾ãliveãªNodeListãArray.prototype.slice.callã§Arrayã«å¤æããã¨ãdocumentã«class=bãªElementã追å ããã¦ããã®Arrayã®è¦ç´ ã¯å¢ããªãï¼ãããå¢ãããæ°æã¡æªãï¼
3) document.querySelectorAll(".c")ã§å¾ãstaticãªNodeListã¯ãdocumentã«class=cãªElementã追å ããã¦ãè¦ç´ ã¯å¢ããªã
4) document.querySelectorAll(".d")ã§å¾ãstaticãªNodeListãArray(ry ã§Arrayã«å¤æããã¨ãå½ç¶ãªããdocumentã«class=dãªElementã追å ããã¦ãè¦ç´ ã¯å¢ããªã
ã¨ããæãã«ãªãã¾ãããããããJavaScriptãã´ãã´ããã¦ãã人éã¨ãã¦Firefoxã¨Chromeã§ãã調ã¹ã¦ããªããã¨ããã®ã¯ãããã¹ããããæ æ ¢ã®ããã§ãããããã¯æ æ ¢ã§ã¯ãªãèªè ã®æ¹ã«ãblogçãèªãã§ãªãã¨ãªãç´å¾ãããããªæ°åã«ãªãããèªåã§æãåããã¦ç¢ºããããã¨ããæªæ¾æã®ãã£ã³ã¹ãæä¾ãã¦ããã ããªã®ã§ãã
ã¯ãã¦ã½ã§ãã
ãããã®åä½ã£ã¦ãgetElementHogeHogeãquerySelectorHogeHogeãå ¨é¨DOMã«å¯¾ãããã¯ã¨ãªãã¨è¦ãªããä¸ã§
liveãªNodeList -> å
ã®ãã¼ã¿ã»ããã«å¤æ´ãããã¨ãã¯ã¨ãªã®çµæã»ããã¨ãã¦ã®NodeListã¯ã¯ã¨ãªã«å¾ã£ã¦å¤åãã
staticãªNodeList -> å
ã®ãã¼ã¿ã»ããã«å¤æ´ããã£ã¦ããã¯ã¨ãªã®çµæã»ããã¨ãã¦ã®NodeListã¯å¤åããªã
ã¨ããæãæ¹ãããæ¹ããããããããã¨æã£ããã©ãã©ãããããããéã«åããã«ããããã¦ã¸ãºãã