2011-07-12 38 views

回答

46

如果scroll down a bit,你看:

警告!在DOM Level 2 事件中引入了接口MutationEvent,但在用戶代理中尚未完全且可互操作地實現 。此外,還有人批評設計中的 接口引入了性能和實施 的挑戰。一個新的規範正在開發中,其目標是 解決了突變事件解決的用例,但更多的是以高性能的方式。因此,本規範描述了突變事件 以作爲傳統行爲的參考和完整性,但不贊成使用MutationEvent接口和MutationNameEvent 接口。

替換API是mutation observers,它是完全指定的in the DOM Living Standard,它取代所有的DOM級別X silliness。

+0

「滾動了起來,」 你的意思。 :-) –

+4

@ TJ - 不下來。上面的是* DOMNodeRemovedFromDocument *。 :-) – RobG

+3

替換將進入DOM級別4 http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#mutation-observers,並且它似乎在Chromium https:/ /中有一些進展。 /bugs.webkit.org/show_bug.cgi?id=73851 –

18

我想更換將突變觀察員:https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

var whatToObserve = {childList: true, attributes: true, subtree: true, attributeOldValue: true, attributeFilter: ['class', 'style']}; 
var mutationObserver = new MutationObserver(function(mutationRecords) { 
    $.each(mutationRecords, function(index, mutationRecord) { 
    if (mutationRecord.type === 'childList') { 
     if (mutationRecord.addedNodes.length > 0) { 
     //DOM node added, do something 
     } 
     else if (mutationRecord.removedNodes.length > 0) { 
     //DOM node removed, do something 
     } 
    } 
    else if (mutationRecord.type === 'attributes') { 
     if (mutationRecord.attributeName === 'class') { 
     //class changed, do something 
     } 
    } 
    }); 
}); 
mutationObserver.observe(document.body, whatToObserve); 
相關問題