簡體   English   中英

文檔上的 mouseenter 和 mouseleave 事件偵聽器在 Firefox 中不起作用

[英]mouseenter and mouseleave event listeners on document not working in Firefox

以下代碼在 Chrome 中正常工作,但在 Firefox 中不能正常工作。 在 Chrome 中,當我在瀏覽器內外移動鼠標時,控制台中會出現一些 output,但在 Firefox 中並非如此。 我的 Firefox 版本是 85.0.2。 任何幫助,將不勝感激。

 document.addEventListener("mouseenter", () => { console.log("mouseenter"); }); document.addEventListener("mouseleave", () => { console.log("mouseleave"); });
 html, body { width: 100vw; height: 100vh; background-color: red; }
 <,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>Document</title> </head> <body> </body> </html>

嘗試為頁面提供尺寸並改用正文

 document.querySelector("body").addEventListener("mouseenter", () => { console.log("mouseenter"); }); document.querySelector("body").addEventListener("mouseleave", () => { console.log("mouseleave"); });
 html, body { width: 100vw; height:100vh; background-color:red; }

事件監聽器的測試

 const addEventListener = EventTarget.prototype.addEventListener; EventTarget.prototype.addEventListener = function(name, handler) { addEventListener.call(this, name, function(event) { window.event = event; handler(event); }) }; function showEvent(e) { // just demonstrating that window.event is set console.log(e.type); } document.addEventListener('mouseenter', showEvent); document.addEventListener('mouseleave', showEvent); document.addEventListener('mouseover', showEvent); document.addEventListener('mouseout', showEvent);
 html, body { width: 100vw; height: 100vh; background-color: red; }
 https://stackoverflow.com/questions/47786133/workaround-for-firefoxs-lack-of-window-event

可能是因為頁面沒有完全加載:

window.onload = (event) => {
  const element = document.getElementsByTagName('body')[0];

  element.addEventListener("mouseenter", () => {
    console.log("mouseenter");
  });
  
  element.addEventListener("mouseleave", () => {
    console.log("mouseleave");
  });
};

現場演示: https://www.w3schools.com/code/tryit.asp?filename=GNQZGHTD6CC9

在 Firefox 上工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM