js监听页面元素变化

JavaScript05

js监听页面元素变化,第1张

该接口用来观察节点变化,MutationObserver是一个构造器,接收一个回调函数callback用来处理节点变化时所做的操作。 var observe = new MutationObserver(mutationCallback)     var observe = new MutationObserver(mutationCallback)      observe.observe(dom, config)// 后面介绍config的配置    var observe = new MutationObserver(mutationCallback)    observe.disconnect()     var observe = new MutationObserver(mutationCallback)     var record = observe.takeRecords() let config = {     attributes: true, //目标节点的属性变化     childList: true, //目标节点的子节点的新增和删除     characterData: true, //如果目标节点为characterData节点(一种抽象接口,具体可以为文本节点,注释节点,以及处理指令节点)时,也要观察该节点的文本内容是否发生变化     subtree: true, //目标节点所有后代节点的attributes、childList、characterData变化 }  <div id="h">123123</div>     <script>             window.onload=function(){                 // Firefox和Chrome早期版本中带有前缀                 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver                 // 选择目标节点                 var target = document.querySelector('#h')                  // 创建观察者对象                 var observer = new MutationObserver(function(mutations) {                       mutations.forEach(function(mutation) {                          console.log(mutation)                      })                  })                  // 配置观察选项:                 var config = { attributes: true, childList: true, characterData: true }                  // 传入目标节点和观察选项                 observer.observe(target, config)                  // 随后,你还可以停止观察                 // observer.disconnect()                 document.getElementById('h').onclick=function(){                 // this.style.width="50px"                 this.innerHTML = "888888"                 }             }     </script> 原文链接:https://blog.csdn.net/weixin_42420703/article/details/98334813

一、页面关闭:

用javascript重新定义 window.onbeforeunload() 事件 

在javascript里定义一个函数即可 

function window.onbeforeunload() { alert("关闭窗口")} 

alert()事件将会在关闭窗口前执行,你也可以用户决定是否关闭窗口 

function window.onbeforeunload() { 

if (event.clientX>document.body.clientWidth &&event.clientY<0 ||event.altKey) 

window.event.returnValue="确定要退出本页吗?" 

2.用onUnload方法 

在body 标签里加入onUnload事件 

body onUnload="myClose()"

二、页面跳转好做,但监听不好做。

页面跳转方法:a.window.location.href='index.html'

b.window.navigate("index.html")

c.

if (confirm("你确定要跳转页面吗?"))...{

window.location.href="index.html" 

}