繁体   English   中英

对于EMBED节点,不会触发MutationObserver回调

[英]MutationObserver callback doesn't fire for EMBED nodes

出于某种原因,以下代码片段仅记录“ false”超过1000次,但从未记录“ true”。 我不知道为什么,因为它似乎适用于任何其他元素。

// create an observer instance
// log whether the mutation is made to an EMBED node
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.dir(mutation.target.nodeName == 'EMBED');
  });
});

// configuration of the observer:
var config = {
  attributes: true, // required
  childList: true, // required
  characterData: true, // required
  subtree: true // get updates from the descendants, not just the children
};

// start observing immediately
// this works because "document" always exists
observer.observe(document, config);

我正在最新的Opera中测试此页上的代码:

https://www.facebook.com/ccstandup/videos/vb.331786510170444/1177307595618327/

(如果使用最新的Opera,它应该是使用EMBED实现的Flash视频,但在其他浏览器中,它可能是VIDEO。)

有什么想法我做错了吗?

target是发生变化的元素。 插入元素时,更改的不是该元素,而是其父元素。 检查mutation.target.nodeName == 'EMBED'将检查embed元素的attributeschildListcharacterData是否已更改,但它们没有更改。 如果需要查看何时插入embed元素本身,则需要从父元素的角度进行查看,例如

var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type !== 'childList') return;

    mutations.addedNodes.forEach(function(node){
      if (node.nodeType !== Node.ELEMENT_NODE) return;

      var embed = node.tagName.toLowerCase() === 'embed' ? node :
          node.querySelector('embed');

      // Found an embed element.
      if (embed) console.log(embed);
    });
    console.dir(mutation.target.nodeName == 'EMBED');
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM