DOMã®å¤æ´ãæ¤ç¥ããã¤ãã³ããéæ¨å¥¨ã«ãªã
代ããã«æ°ããä½ãããMutationObserverãªããã®ã
MutationObserver - Web API インターフェイス | MDN
https://developer.mozilla.org/ja/docs/Web/API/MutationObserver
DOMå¤æ´æ¤ç¥ã¯çµæ§ãã使ããã®ï¼ç¹ã«ã¦ã¼ã¶ã¼ã¹ã¯ãªããï¼ãªã®ã«
æ¥æ¬èªã®è§£èª¬ãµã¤ããâã®MDNã¨MSDNãここããããããªã
ãã®é½åº¦ã°ã°ã£ããææ¢ãã§çã£ç«¯ãã試ãã®ãé¢åãªã®ã§ä»¥ä¸ã«èªåç¨ã«ã¾ã¨ããã
ã³ã³ã¹ãã©ã¯ã¿
ã¾ãã¤ã³ã¹ã¿ã³ã¹ãä½ãã
window.MutationObserverã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã«ãDOMå¤æ´ãæ¤ç¥ããéã«å®è¡ããé¢æ°ã渡ãã
function omega(data1, data2) { alert("ï¾ï¾ï¾ï¾ï½¼ï½±ï¾ ï¾"); console.log(data1); console.log(data2); } var mo = new MutationObserver(omega);
ããã§å¼ã³åºãããé¢æ°ã«ã¯
第ä¸å¼æ°ã«å¤æ´ãããDOMã®è©³ç´°ãå
¥ã£ãé£æ³é
åï¼MutationRecordsã¨å¼ã°ããï¼ãå«ãé
åãã
第äºå¼æ°ã«å¼ã³åºãå
ã®MutationObserverã®ã¤ã³ã¹ã¿ã³ã¹ãå
¥ãã
ã¡ã½ãã
observe
ã¤ã³ã¹ã¿ã³ã¹ãä½æããã ãã§ã¯ç£è¦ã¯è¡ãããªãã
observeã¡ã½ãããå®è¡ãã¦åãã¦ç£è¦ãå§ã¾ãã
var pikaman = document.getElementById("pikaman"), options = {childList: true}; var mo = new MutationObserver(omega); mo.observe(pikaman, options);
第ä¸å¼æ°ã«ç£è¦å¯¾è±¡ã®ãã¼ãã第äºå¼æ°ã«ä½ãã©ãã¾ã§ç£è¦ãããã®ãªãã·ã§ã³ãæ¸ããé£æ³é
åãå
¥ããã
æå®ã§ãããªãã·ã§ã³ã¯ä»¥ä¸ã®éãã
åºæ¬ãªãã·ã§ã³
åå | å | ç£è¦å¯¾è±¡ |
---|---|---|
attributes | çå½å¤ | 対象ãã¼ãã®å±æ§å
¨è¬ ï¼å±æ§å/å±æ§å¤ã¯åããªãï¼ |
characterData | çå½å¤ | 対象ãã¼ãã®åã¨ãªãããã¹ããã¼ã |
childList | çå½å¤ | 対象ãã¼ãã®åãã¼ã |
ã©ããããã©ã«ãã§falseãtrueã§æå¹åã
ããã§è¨ãç£è¦ã¨ã¯ããæ°è¦/å¤æ´/åé¤ãå
¨ã¦ã®ã¢ã¯ã·ã§ã³ãæãã
ä¸è¨ã®3ã¤ãåºæ¬ã«ãã¦ãã©ãã1ã¤å¿
é ã
ãããªãã¨ä¸è¨ã®è¿½å ãªãã·ã§ã³ã¯æå®ã§ããªãã
追å ãªãã·ã§ã³
åå | å | å¿ é | å¹æ |
---|---|---|---|
attributeFilter | é å | attributes | ç£è¦ããå±æ§åãéå®ãã |
attributeOldValue | çå½å¤ | attributes | è¨é²ããDOMãã¼ã¿ã«å¤æ´åã®å±æ§ãã¼ã¿ãå ãã |
characterDataOldValue | çå½å¤ | characterData | è¨é²ããDOMãã¼ã¿ã«å¤æ´åã®ããã¹ããã¼ããå ãã |
subtree | çå½å¤ | childList | 対象ãã¼ãã®åå«ãã¼ããç£è¦ãã |
- attributeFilter
ç£è¦ããå±æ§åãæå®ãããã®ã ãã«çµãã
ä¾ãã°aã¿ã°ã®hrefã¨targetã®å¤æ´ã ããã£ããã£ãããå ´åã¯ä»¥ä¸ã®ããã«ããã
var attr = ["href", "target"], options = {attributes: true, attributeFilter: attr}; var mo = new MutationObserver(omega); mo.observe(pikaman, options);
- attributeOldValue
MutationRecordsã®oldValueã¨ããããããã£ã«å¤æ´åã®å±æ§å¤ãè¨é²ããããã«ããã
- characterDataOldValue
åä¸ãå±æ§å¤ã®ä»£ããã«å¤æ´åã®ããã¹ããã¼ããè¨é²ã
- subtree
対象ã®åãã¼ãã ãã§ãªããåå«ï¼ãã®é
ä¸ã«ããå
¨ã¦ã®åãã¼ãï¼ã®å¤æ´ããã£ããããã«ããã
ããã使ãã«ã¯childListæå®å¿
é ã
ãããç¥ããªãã£ãç§ã¯{subtree: true}
ã¨ã ãæ¸ãããªãã·ã§ã³ã渡ãã¦
ãªãã§åããªãã®ãã¨è»½ãè©°ã¾ã£ãâ¦ã
MutationObserverã§ç½®ãæããããDOMã¤ãã³ã
DOMã¤ãã³ã | MutationObserverãããã㣠ï¼æ¬å¼§å ã¯ä»»æï¼ |
---|---|
DOMAttrModified | attributes: true (, attributeOldValue: true) (, attributeFilter: ["å±æ§å"]) |
DOMAttributeNameChanged | attributes: true (, attributeOldValue: true) (, attributeFilter: ["å±æ§å"]) |
DOMCharacterDataModified | characterData: true (, characterDataOldValue: true) |
DOMNodeInserted | childList: true (, subtree: true) |
DOMNodeInsertedIntoDocument | childList: true (, subtree: true) |
DOMNodeRemoved | childList: true (, subtree: true) |
DOMNodeRemovedFromDocument | childList: true (, subtree: true) |
DOMSubtreeModified | childList: true, subtree: true |
disconnect
observeã§éå§ããç£è¦ãä¸æ¢ããã
var mo = new MutationObserver(omega); mo.observe(pikaman, options); mo.disconnect();
å½ç¶ã ããç£è¦ããããã»ã¹ãå¢ããã°å¢ããã»ã©è² è·ããããã
ç£è¦ããå¿
è¦ãç¡ããªã£ããdisconnectãã¦è§£æ¾ãããã
takeRecords
ããã¾ã§observeã§è¨é²ãããMutationRecordsã®å
容ãå
¨ã¦é
åã¨ãã¦è¿ãã
ã¨åæã«ãããã¾ã§ã®è¨é²ãã¯ãªã¢ããã
var mo = new MutationObserver(omega); mo.observe(pikaman, options); console.log( mo.takeRecords() );
MutationRecords
è¨é²ããããã¼ã¿ä¸è¦§ã
åå | å | æ»ãå¤ |
---|---|---|
attributeName | æåå | å¤æ´ãããå±æ§å¤ãå±ããå±æ§å |
attributeNamespace | æåå | å¤æ´ãããå±æ§åã®åå空é |
addedNodes | ãã¼ããªã¹ã | 追å ããããã¼ãã®ä¸è¦§ |
removedNodes | ãã¼ããªã¹ã | åé¤ããããã¼ãã®ä¸è¦§ |
previousSibling | ãã¼ã | 追å (åé¤)ããããã¼ãã®ç´åã«ãããã¼ã |
nextSibling | ãã¼ã | 追å (åé¤)ããããã¼ãã®ç´å¾ã«ãããã¼ã |
target | ãã¼ã | å¤æ´ããããã¼ã |
type | æåå | åºæ¬ãªãã·ã§ã³ã®å 容ï¼attributesãcharacterDataãchildListï¼ |
oldValue | æåå | ä¸è¿°ã®è¿½å ãªãã·ã§ã³ã§æå®ããå 容 |
åä½ç°å¢
ã§ãåé¡ã®ãã©ã¦ã¶å¯¾å¿ç¶æ³ã ã
Firefoxã¨Chromeã¯ã¨ã£ãã®åã«å¯¾å¿æ¸ã¿ã
IEã¯11ãããOperaã¯Blinkçãããï¼Prestoçé対å¿ï¼
IE11ã®ã·ã§ã¢ã伸ã³ã¦ããã¨ã¯è¨ããã¾ã ã¾ã IE10ãå¤ãã®ãç¾å®ã
ãã©ã¼ã«ããã¯ããã®ãé¢åã ããjQueryã®.on()ã§æ軽ã«ä½¿ããã¨ããç¹ã§ã
çµå±DOMã¤ãã³ãã«çãã¡ãããã ããããã