JS

在线预览

使用《Web API 接口》的《MutationObserver》 MutationObserver

网上查到的很多都是使用 Mutation events 的,但在 MDN 上一查这个事件已经废弃了,并且推荐用 MutationObserver 替换掉

Deprecated This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Mutation events provide a mechanism for a web page or an extension to get notified about changes made to the DOM. Use Mutation Observers instead if possible.

1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <meta charset="UTF-8" /> 5 <title>Document</title> 6 </head> 7 <body> 8 <h1>下述方法也可以监听使用《谷歌页面翻译》时的DOM变化</h1> 9 <h1> 10 The following way also can catch changes of DOM when using 11 <em>Google page translation</em> 12 </h1> 13 <h3>使用MutationObserver(Using MutationObserver)</h3> 14 <div id="hello"><span>Hello</span> <em>Mr.</em> Word!</div> 15 <button onclick="addNode()">添加(add DOM)</button> 16 <script> 17 // Select the node that will be observed for mutations 18 var targetNode = document.querySelector("body"); 19 20 // Options for the observer (which mutations to observe) 21 // subtree:是否监听子节点的变化 22 var config = { attributes: true, childList: true, subtree: true }; 23 24 // Callback function to execute when mutations are observed 25 var callback = function(mutationsList) { 26 for (var mutation of mutationsList) { 27 console.log(mutation); 28 if (mutation.type == "childList") { 29 console.log("A child node has been added or removed."); 30 } else if (mutation.type == "attributes") { 31 console.log( 32 "The " + mutation.attributeName + " attribute was modified." 33 ); 34 } 35 } 36 }; 37 38 // Create an observer instance linked to the callback function 39 var observer = new MutationObserver(callback); 40 41 // Start observing the target node for configured mutations 42 observer.observe(targetNode, config); 43 44 // // Later, you can stop observing 45 // observer.disconnect(); 46 47 var addNode = function() { 48 var divnode = document.createElement("div"); 49 var textNode = document.createTextNode("Hello world!"); 50 divnode.appendChild(textNode); 51 document.getElementById("hello").appendChild(divnode); 52 }; 53 </script> 54 </body> 55</html>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593