DOMNodeInserted
is being deprecated, along with the other DOM mutation events, because of performance issues - the recommended approach is to use a MutationObserver to watch the DOM.
(由于性能问题,不建议使用DOMNodeInserted
以及其他DOM突变事件-推荐的方法是使用MutationObserver来监视DOM。)
It's only supported in newer browsers though, so you should fall back onto DOMNodeInserted
when MutationObserver
isn't available.(不过,只有新的浏览器才支持它,因此,当MutationObserver
不可用时,您应该使用DOMNodeInserted
。)
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (!mutation.addedNodes) return
for (var i = 0; i < mutation.addedNodes.length; i++) {
// do things to your newly added nodes here
var node = mutation.addedNodes[i]
}
})
})
observer.observe(document.body, {
childList: true
, subtree: true
, attributes: false
, characterData: false
})
// stop watching using:
observer.disconnect()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…