ã¯ããã«
IE8 ã«ã¯ Selectors API ã¨ããæ°ããä»æ§ãå®è£
ããã¾ããã
ã¨ãããã¨ã§ãä»å¾ DOM æä½ API ã®ä¸»æµã«ãªãã§ããã Selectors API ã«ã¤ãã¦ã¾ã¨ãã¦ããã¾ãã
Selectors API ã使ãããã©ã¦ã¶
2008 å¹´ 3 æ 6 æ¥ç¾å¨ã®ä¸è¦§
- WebKit Build Archives | WebKit (éçºçã® Safari)
- Windows | Official Site for Microsoft Windows 10 Home & Pro OS, laptops, PCs, tablets & more (IE8 ã® Beta ç)
Selectors API ã¨ã¯
Selectors API ã¨ã¯ W3C ã§å®ç¾©ãããä»æ§ã§ãã詳細ã«é¢ãã¦ã¯こちらãã©ãã
ç°¡åã«èª¬æããã¨
getElementsByTagName ã getElementsByName ã getElementById ãªã©ã® DOM ã®ãªãã¸ã§ã¯ããåå¾ããã¡ã½ããããã¾ãããã
ããã®ããããçãã® querySelector ã querySelectorAll ã¨ããã¡ã½ããã®ãã¨ãæãã¾ãã
ãã¨ãã°ãæ¢åã® getElementsByTagName ãªã©ã®ã¡ã½ããã®ä»£ããã« Selectors API ã使ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
// 以ä¸ã®äºã¤ã¯åãè¦ç´ ãåå¾ãã var nl00 = document.getElementsByTagName('div'); var nl01 = document.querySelectorAll('div'); alert(nl00[0] == nl01[0]); // true alert(nl00[1] == nl01[1]); // true : : // 以ä¸ã®äºã¤ã¯åãè¦ç´ ãåå¾ãã var elm00 = document.getElementById('target'); var elm01 = document.querySelector('#target'); alert(elm00 == elm01); // true // 以ä¸ã®äºã¤ã¯åãè¦ç´ ãåå¾ãã var nl02 = document.getElementsByName('hoge'); var nl03 = document.querySelectorAll('*[name="hoge"]'); alert(nl02[0] == nl03[0]); // true alert(nl02[1] == nl03[1]); // true : :
ã©ãã§ããï¼ç°¡åã§ããï¼
querySelector ã querySelectorAll ã«ã¯ã CSS ã»ã¬ã¯ã¿ã渡ãã
åã®ä¾ãè¦ã¦æ°ãä»ãæ¹ãå¤ãã¨æãã¾ããã querySelector ã querySelectorAll ã«ã¯ CSS ã»ã¬ã¯ã¿ã渡ãã¾ãã
CSS ã»ã¬ã¯ã¿ã¨ã¯ã CSS ã®ä¸ã«ãã³ã®åã«æ¸ãããè¦ç´ ãç¹å®ããããã®æååã®ãã¨ã§ããã
ãã¨ãã°ã以ä¸ã®ä¾ã§è¨ãã¨
ul.navigation li { display: inline; }
ãul.navigation liãã CSS ã»ã¬ã¯ã¿ã«ãããã¾ãã
(追è¨) çä¼¼ã¯ã©ã¹ã®æ±ã
id:vantguarde ãããã以ä¸ã®ãããªã¯ã¦ãã³ã¡ã³ããé ãã¾ããã®ã§ãçä¼¼ã¯ã©ã¹ã«ã¤ãã¦è¿½è¨ãã¾ããid:vantguarde ãããããã¨ããããã¾ãï¼
æ¬ä¼¼ã¯ã©ã¹ãªãã¼ããåå¾ã§ããã£ã¦ã®ãæ¸ãã¦ã»ããããã
はてなブックマーク - vantguardeのブックマーク / 2008年3月6日
ãã£ãããã¨ãã Selectors API ã§ã¯çä¼¼ã¯ã©ã¹ã«ã対å¿ãã¦ãã¾ãã
ãã¨ãã°ã
// ç¾å¨é¸æããã¦ããè¦ç´ ãåå¾ var elm00 = document.querySelector(':focus'); // ç¾å¨ãã¦ã¹ãä¹ã£ãã£ã¦ãè¦ç´ ãåå¾ var elm01 = document.querySelector(':hover'); // elementFromPoint ã¨ç価
ãã®ãããªæãã«ãªãã¾ãã
ãã ããã©ã¤ãã·ã¼ã®é
æ
®ãåãªããã°ãªã®ãã¯åããã¾ããã IE8 ã§ã¯ :visited çä¼¼ã¯ã©ã¹ã¯åå¾ã§ãã¾ããã§ããã
alert(document.querySelectorAll('a:visited').length); // 常㫠0
(追è¨) ã«ã³ãã§è¤æ°ã® CSS ã»ã¬ã¯ã¿ãæå®ã§ãã¾ãã
ã«ã³ãèªä½ã CSS ã»ã¬ã¯ã¿ã®ä»æ§ã®ä¸é¨ãªã®ã§ãå½ããåã¨è¨ãã°å½ããåã§ããããããæ¸ãã¦ããã¾ãã
// 以ä¸ã®ããã«ãã«ã³ãã使ã£ã¦è¤æ°ã® CSS ã»ã¬ã¯ã¿ãæå®ãããã¨ãã§ãã¾ãã document.querySelectorAll('span, strong'); // span 㨠strong è¦ç´ ããã¹ã¦åå¾
querySelector 㨠querySelectorAll ã®éã
querySelector 㨠querySelectorAll ã®éãã¯ãããããæåã®ä¾ã§åããã¨æãã¾ãããåå¾ããè¦ç´ ãä¸åãè¤æ°ãã®éãã§ãã
querySelector ã®å ´åã¯ããã®ã»ã¬ã¯ã¿ãé¸æããè¦ç´ ã®ä¸çªæåã®è¦ç´ ã ããåå¾ãã querySelectorAll ã®å ´åã¯ãã¹ã¦ã®è¦ç´ ãåå¾ãã¾ãã
ä¾ãã°ã以ä¸ã®ãã㪠HTML ããã£ãã¨ãã¾ãã
<html> ..(ç¥).. <body> <ul><li>aaa</li><li>bbb</li><li>ccc</li></ul> </body> </html>
ããããã¨ã querySelector ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
var elm = document.querySelector('body > ul > li'); alert(elm.innerHTML); // aaa
ãã®ããã«ã querySelector 㯠HTML ãä¸ããé *1ã«è¦ã¦ä¸çªæåã«åºã¦æ¥ãè¦ç´ ãé¸æãã¾ãã
ã¾ãã querySelectorAll ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
var nl = document.querySelectorAll('body > ul > li'); alert(nl.length); // 3 alert(nl[0].innerHTML); // aaa alert(nl[1].innerHTML); // bbb alert(nl[2].innerHTML); // bbb
ãã®ããã«ã querySelectorAll ã HTML ãä¸ããé *2ã«æ ¼ç´ãã¦ãã¾ãã
ã¤ã¾ãã
document.querySelector(selector) // 㯠document.querySelectorAll(selector)[0] // ã¨ç価
ã¨ãããã¨ã§ã*3ã
querySelectorAll ã§åå¾ãããå¤
次ã¯ã querySelectorAll ã§åå¾ãããå¤ã«ã¤ãã¦ã§ãã
ãã®å¤ã¯ãé
åã®ããã«æ±ãã¾ãããé
åã§ã¯ããã¾ããã
// ãã¹ã¦ã®è¦ç´ ãåå¾ var nl = document.querySelectorAll('*'); alert(nl instanceof Array); // false // push ãªã©ã®é åã®ã¡ã½ããã¯ä½¿ããªã try { nl.push('hoge') } catch (e) { alert('error') } // error // 使ããã®ã¯ length ã ã alert(nl.length);
ããã¾ã§ã¯ãå¾æ¥ã® getElementsByTagName ãªã©ã§åå¾ãããå¤ã¨åãã§ãã
ãããã querySelectorAll ã§åå¾ãããå¤ã®æ§è³ªã¯ getElementsByTagName ãªã©ã§åå¾ãããå¤ã¨ãå°ãéã£ã¦ãã¾ãã
// ãã¹ã¦ã®è¦ç´ ãåå¾ var nl00 = document.getElementsByTagName('*'); var nl01 = document.querySelector('*'); // ããã§ã HTML ã®å 容ãæ¸ãæããï¼è¦ç´ ãä¸ã¤åé¤ããï¼ document.body.removeChild(document.body.getElementsByTagName('*')[0]); alert(nl00.length == nl01.length); // false alert(nl00.length); // ãã¡ãã¯ãå¤æ´ãåæ ããã¦ä¸ã¤å°ãªãå¤ã«ãªã alert(nl01.length); // ãã¡ãã¯ãå¤æ´ãåæ ãããªãï¼åå¾æã®ç¶æ ï¼
ãã®ããã«ãåå¾å¾ã« HTML ã®æ§é ãå¤åããå ´åãã®å¤åãåæ ãããããããªããã®éããããã¾ãã
ã¾ããããããã®ãªãã¸ã§ã¯ãã®ååã NodeList ã StaticNodeList ã¨ããã¾ãã
NodeList | getElementsByTagName ãªã©ã§åå¾ãããå¤ | å¤åå¾å¾ã® HTML ã®æ§é ã®å¤åããã®å ´ã§åæ ãã |
StaticNodeList | querySelector ã§åå¾ãããå¤ | åå¾æã®ç¶æ ã®ã¾ã¾ã§ãå¤åãåæ ãããªã |
ä¸è¦ã getElementsByTagName ã®ä»æ§ã®ã»ãã便å©ã«æãã¾ããããã®åçãªæ§è³ªããã£ãããã§å¦çãéããªã£ã¦ãããã§ããã
ã ããã querySelectorAll ã¯åå¾ããå¤ã®æä½ã軽ãããããã¾ãããã
document ã ããããªããè¦ç´ ããã使ãã¾ãã
ä»ã¾ã§ã¯ã document.querySelectorAll ã¨ãã£ã¦ãã¾ããããæ®éã«è¦ç´ ã§ã使ãã¾ãã
è¦ç´ ã§ãå®è¡ããå ´åã¯åå«è¦ç´ ããè¦ç´ ãé¸æãããããã«ãªãã¾ãã
var elm = document.querySelector('#target'); // elm ã®åå«è¦ç´ 㧠hoge ã¨ããã¯ã©ã¹ãæã¤è¦ç´ elm.querySelectorAll('.hoge'); // ã¤ã¾ãã #target .hoge ã¨åã
ã¾ã¨ã
ãããªæãã§ãï¼è¶
便å©ã§ããï¼
DOM 3 XPath æ´¾ãªåã§ããããã£ã±ã IE8 ã Selectors API ãå®è£
ããã®ã§ããããã Selectors API ã®æ代ãæ¥ãã¨æãã¾ãã
ä»ã®ãã©ã¦ã¶ãæ©ãå®è£
ãã¦æ¬²ããã§ããï¼
ãã®ä¾¿å©ãªã¡ã½ããã使ããããã«ãªãã®ã¯é常ã«æ¥½ãã¿ã§ãï¼ï¼
ã§ã¯ã§ã¯ï¼
ï¼Namespace ã«ã¤ãã¦ã¯çç¥ãã¾ãããï¼