变异观察者---子树

时间:2012-05-12 01:33:50

标签: webkit chromium mutation-observers

我正在阅读此http://lists.w3.org/Archives/Public/public-webapps/2011JulSep/1622.html,而Chrome的行为似乎与规格形成鲜明对比。如果我正确理解了规范,则为元素定义“子树”意味着应该报告对该元素的子树(包括元素本身)的更改。但是,在执行这段代码时,我什么都没得到。

var observer = new WebKitMutationObserver(function(e){console.log(e);})
observer.observe(document.body, {subtree:true, attributes:true});
document.body.appendChild(document.createElement('div'));

我错过了什么?有人可以详细说明这个吗? 谢谢!

3 个答案:

答案 0 :(得分:11)

文档不清楚,但除非您同时指定subtree,否则会忽略childList:true

attributesattributeFilter的情况相同。

希望它仍有帮助。

答案 1 :(得分:4)

根据this article

  

childList :如果要观察目标节点的子元素(包括文本节点)的添加和删除,则设置为true。

     

子树:如果要观察不仅仅是目标的突变,还要观察目标的后代,则设置为true。

这也解释了子树,具体取决于 childList

答案 2 :(得分:1)

mutationObserver配置中,attributescharacterDatachildList中的至少一个需要设置为true

现在,如果仅设置childList: true,它将仅观察目标元素的直接子级(深度1),而不观察完整的子树。

要观察完整的子树,childListsubtree都需要设置true

例如

{
   childList: true,
   subtree: true
}

我希望这会有所帮助。