window.event
是一个由微软 IE 引入的属性,只有当 DOM 事件处理程序被调用的时候会被用到。它的值是当前正在处理的事件对象。
据实际测验发现,window.event 在异步事件触发的时候,才会有值,否则为 undefined,且在一个事件循环之中,如果下一个宏任务开始,下一个宏任务中是获取不到的。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>event</title> <style> body{ margin:20px; } #btn1,#btn2{ width:300px; height:300px; background-color: palegoldenrod; margin-bottom: 10px; } </style> </head> <body> <div id="btn1"> 点击区域1 </div> <div id="btn2"> 点击区域2 </div> <script> let id1= document.getElementById("btn1") let id2= document.getElementById("btn2") console.log("window.event0",window.event) // undefined id1.addEventListener("click",(event)=>{ console.log("event1",event) // PointerEvent console.log("window.event1",window.event) // PointerEvent }) id2.addEventListener("click",(event)=>{ let pro1 = new Promise((resolve,reject)=>{ console.log("event2",event) console.log("window.event2",window.event) resolve() }) pro1.then(()=>{ console.log("then") console.log("window.event3",window.event) }).finally(()=>{ console.log("window.event4",window.event) // PointerEvent setTimeout(()=>{ console.log("window.event5",window.event) // undefined },1000) }) }) </script> </body> </html>
标签:console,PointerEvent,获取,window,let,event,log From: https://www.cnblogs.com/beileixinqing/p/16802301.html