我正在為 chrome 創建一個擴展,我有一個問題。我想知道,如何從元素中洗掉事件?這是我的事件處理程式:
const handler = function (e) {
console.log("on");
};
我希望我的擴展程式添加一個mousemove事件來html標記。我做到了。
document.querySelector("html").removeEventListener("mousemove", handler);
我想做一些事情,當用戶單擊我的擴展圖示時,如果徽章文本打開,則從html;中洗掉事件偵聽器 但我不知道如何從html標簽中洗掉事件監聽器!我試過了,但我做不到。
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: myFuntion,
args: ["remove"],
});
我試過removeEventListener了,但沒有用。我在全域范圍內定義了我的函式,但它沒有作業,我得到一個錯誤,你的函式未定義。我什至嘗試將功能存盤在 chrome 存盤中!等等,但是在為 chrome 開發擴展程式時,如何從 html 標記中洗掉事件偵聽器?謝謝你的幫助。
uj5u.com熱心網友回復:
當executeScript注入的代碼運行時,每次都會創建一個新函式,所以之前的函式不能被洗掉,因為它的內部指標現在不同了。
您可以通過以下方式保留該功能window:
window.handler = window.handler || function (e) {
console.log("on");
};
然后您可以稍后在另一個 executeScript 中將其洗掉:
document.querySelector("html").removeEventListener("mousemove", window.handler);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/413053.html
標籤:
