, .
id
, , : observer.observe
, , - , . DOM , , , , :
var target = document.querySelector("#foo");
var observer = new MutationObserver(function(mutations) {
snippet.log("#foo was modified");
});
observer.observe(target, {subTree: true, childList: true});
var counter = 0;
tick();
setTimeout(function() {
snippet.log("Replacing foo with a new one");
target.parentNode.removeChild(target);
target = document.createElement("div");
target.id = "foo";
target.innerHTML = "This is the new foo";
document.body.insertBefore(target, document.body.firstChild);
}, 1000);
function tick() {
target.appendChild(document.createTextNode("."));
if (++counter < 20) {
setTimeout(tick, 200);
}
}
<div id="foo">This is the original foo</div>
<script src="//tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script>
Hide result, DOM, , , , :
var target = document.querySelector("#foo");
var observer = new MutationObserver(function(mutations) {
snippet.log("#foo was modified");
});
observer.observe(target, {subTree: true, childList: true});
var counter = 0;
tick();
setTimeout(function() {
snippet.log("Removing foo for a moment");
target.parentNode.removeChild(target);
setTimeout(function() {
snippet.log("Putting the same foo back in");
document.body.insertBefore(target, document.body.firstChild);
}, 100);
}, 1000);
function tick() {
target.appendChild(document.createTextNode("."));
if (++counter < 20) {
setTimeout(tick, 200);
}
}
<div id="foo">This is the original foo</div>
<script src="//tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script>
Hide result, , observe
.
, :
, ?
, :
observer.disconnect();
, :
observer.observe(theNewElement, );
? , , , " "?
, , , , , , , . , , observer disconnect
, , . , . , .
, , , .
"", . , . , , . , , .