2016-11-01 5 views
1

DOMNodeInserted event has been deprecatedおよびすべての突然変異イベントは突然変異観察者に置き換えられました。新しいDOMNodeInsertedに相当するものは何ですか?

しかし、within mutation observersには、ノードの属性、コンテンツ、および子への突然変異を監視するための設定オプションしかありません。 DOMツリー内の配置に関する突然変異については何もありません。

document.bodyから追加されたターゲットノードである1つの突然変異を検索することは、すべてを観察するという "解決策"が1つありますが、それは何かを行うにはかなりひどい方法です。

推奨されていないDOMNodeInsertedを突然変異観測員に置き換えるには、どのような方法がありますか?

EDIT:The searchbar works better with Tags than Questions. Explaining this could reduce duplicates and save users time以下

使用可能な突然変異オブザーバの設定オプションのいずれもページに追加された要素に反応しないことを確認することができますいくつかのコードです:もう一度、SOはWhat is the most efficient way of detecting when an element with a particular ID is inserted in the DOM

重複ことが可能であることが見出さ:

var addMe = document.createElement("div"); 
 
var config = { 
 
    attributes: true, 
 
    childList: true, 
 
    characterData: true 
 
} 
 
var observer = new MutationObserver(function(mutations) { 
 
    console.log(mutations); 
 
}); 
 
observer.observe(addMe, config); 
 

 
function appendAddMe() { 
 
    document.body.appendChild(addMe); 
 
}
div { 
 
    height: 50px; 
 
    width: 50px; 
 
    background: #969; 
 
}
<button onclick="appendAddMe()">Append addMe</button>

答えて

0

私はこれを実際にと考えていません。質問にはの回答がありますが、それは何よりも優れているので、とにかくそれを共有したいと思います。特にWhat is the most efficient way of detecting when an element with a particular ID is inserted in the DOM

、この回答:https://stackoverflow.com/a/25107064/4808079

schettino72からiframe要素は、ロード/ unoadイベントをトリガ

この質問は、いくつかの良い答えを持っています。あなたが見たい要素にダムのiframeを挿入することができます...そして、元の要素の 'load'イベントを自分でトリガします。

function watch($ele){ 
    var $iframe = document.createElement('iframe'); 
    $iframe.style.display = 'none'; 
    $ele.appendChild($iframe); 
    $iframe.addEventListener('load', function(){ 
     var event = new CustomEvent('load'); 
     $ele.dispatchEvent(event); 
    }); 
} 

私はまだ、これはハックではなく、真の答えであるとしてオープンこの質問を維持したいと思います。 DOMNodeInsertedはおそらく廃止されていないので、誰もがこのようにすることができます。

関連する問題