teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

調整

2021/09/14 02:58

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -30,4 +30,11 @@
30
30
  mo.observe(document.body, opt);
31
31
  });
32
32
  </script>
33
+ ```
34
+
35
+ ちなみにArray.fromにこだわりがなければこうした方がわかりやすいと思います
36
+ ```javascript
37
+ const mo = new MutationObserver(function(r){
38
+ console.log([...r].map(x=>[...x.addedNodes].filter(y=>y instanceof HTMLElement)[0].id));
39
+ });
33
40
  ```

1

chousei

2021/09/14 02:57

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -1,1 +1,33 @@
1
- 追加されたことを感知したいならMutationObserverInitをご利用ください
1
+ 追加されたことを感知したいならMutationObserverInitをご利用ください
2
+
3
+ # sample
4
+ ```javascript
5
+ <script>
6
+ const loop=5;
7
+ const max=10;
8
+ let count=0;
9
+ let timerId;
10
+ let j=0;
11
+ window.addEventListener('DOMContentLoaded', ()=>{
12
+ timerId=setInterval(()=>{
13
+ for(let i=0;i<max;i++){
14
+ div = document.createElement('div');
15
+ div.id="div"+(i+j);
16
+ div.style.width = "200px";
17
+ div.style.height = "30px";
18
+ div.style.background="red";
19
+ document.body.appendChild(div);
20
+ }
21
+ j+=max;
22
+ if(++count>=loop) clearInterval(timerId);
23
+ },1000);
24
+ const mo = new MutationObserver(function(r){
25
+ Array.from(r,x=>console.log([...x.addedNodes].filter(y=>y instanceof HTMLElement)[0].id));
26
+ });
27
+ const opt = {
28
+ childList:true,
29
+ };
30
+ mo.observe(document.body, opt);
31
+ });
32
+ </script>
33
+ ```