-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmutationobserver-addchildren.html
56 lines (49 loc) · 1.44 KB
/
mutationobserver-addchildren.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<title>Observing the addition of child nodes</title>
<link rel="stylesheet" href="mutationobserver.css">
</head>
<body>
<header>
<h1>Observing the addition of child nodes</h1>
<nav><a href="http://dev.opera.com/articles/view/mutation-observers-tutorial/">Return to article</a></nav>
</header>
<article>
<p>In this example, clicking the <b>Add a node</b> button adds another paragraph element node to the document tree. Open the developer console to see what each mutation record reports.</p>
<p><button>Add a node</button></p>
</article>
<div id="demo"></div>
<script>
(function(d){
var btn = d.querySelector('button'),
demo = d.getElementById('demo'),
clickhandler = function(){
var p, sentence = 'Lorem ipsum dolar sit amet consecutum.';
if( p !== undefined){
p = p.cloneNode(true);
} else {
p = d.createElement('p');
p.textContent = sentence;
}
demo.appendChild(p);
}
btn.addEventListener('click',clickhandler,false);
var mocallback = function(mutationrecords){
mutationrecords.map( function(mr){
var mrtype = [];
mrtype[0] = Object.prototype.toString.call(mr)+"\n";
for( var o in mr ){
mrtype.push( o +': '+mr[o] );
}
console.log( mrtype.join('\n') );
});
},
mo = new MutationObserver(mocallback),
demo = d.getElementById('demo');
mo.observe(d.body,{'childList':true,'subtree':true});
})(document);
</script>
</body>
</html>