回答編集履歴

2

調整

2021/09/14 02:58

投稿

yambejp
yambejp

スコア116724

test CHANGED
@@ -63,3 +63,17 @@
63
63
  </script>
64
64
 
65
65
  ```
66
+
67
+
68
+
69
+ ちなみにArray.fromにこだわりがなければこうした方がわかりやすいと思います
70
+
71
+ ```javascript
72
+
73
+ const mo = new MutationObserver(function(r){
74
+
75
+ console.log([...r].map(x=>[...x.addedNodes].filter(y=>y instanceof HTMLElement)[0].id));
76
+
77
+ });
78
+
79
+ ```

1

chousei

2021/09/14 02:57

投稿

yambejp
yambejp

スコア116724

test CHANGED
@@ -1 +1,65 @@
1
1
  追加されたことを感知したいならMutationObserverInitをご利用ください
2
+
3
+
4
+
5
+ # sample
6
+
7
+ ```javascript
8
+
9
+ <script>
10
+
11
+ const loop=5;
12
+
13
+ const max=10;
14
+
15
+ let count=0;
16
+
17
+ let timerId;
18
+
19
+ let j=0;
20
+
21
+ window.addEventListener('DOMContentLoaded', ()=>{
22
+
23
+ timerId=setInterval(()=>{
24
+
25
+ for(let i=0;i<max;i++){
26
+
27
+ div = document.createElement('div');
28
+
29
+ div.id="div"+(i+j);
30
+
31
+ div.style.width = "200px";
32
+
33
+ div.style.height = "30px";
34
+
35
+ div.style.background="red";
36
+
37
+ document.body.appendChild(div);
38
+
39
+ }
40
+
41
+ j+=max;
42
+
43
+ if(++count>=loop) clearInterval(timerId);
44
+
45
+ },1000);
46
+
47
+ const mo = new MutationObserver(function(r){
48
+
49
+ Array.from(r,x=>console.log([...x.addedNodes].filter(y=>y instanceof HTMLElement)[0].id));
50
+
51
+ });
52
+
53
+ const opt = {
54
+
55
+ childList:true,
56
+
57
+ };
58
+
59
+ mo.observe(document.body, opt);
60
+
61
+ });
62
+
63
+ </script>
64
+
65
+ ```