我正在 chrome 擴展中開發一項功能,允許用戶將鼠標懸停在頁面上并檢測任何元素。頁面右上角的選擇按鈕可激活該功能。每當點擊一個元素時,按鈕附近的輸入框就會填充該元素的innerHTML。單擊后,選擇應停止,并且該功能將不再識別單擊。一切正常,但我無法解除點擊事件的綁定。我的代碼有什么問題嗎?請告訴我內容.jswindow.onload = () => { var highlight = function (event){ if(!$(event.target).is("#home_container *")){ event.target.style.backgroundColor = "rgba(121, 204, 255, 0.4)"; } } var remove = function (event){ if(!$(event.target).is("#home_container *")){ event.target.style.backgroundColor = ""; } } var disableLinks = function (event){ event.preventDefault(); } var highlightProcess = function(event) { $('a').bind("click", disableLinks); $(document).bind("mouseover", highlight); $(document).bind("mouseout", remove); $(document).bind("click", (elem) => { if(!$(elem.target).is("#home_container *")){ $("#input_box").val(elem.target.innerHTML); remove(elem); $(document).unbind("mouseover", highlight); //<-- works $(document).unbind("mouseout", remove); //<-- works $('a').unbind("click", disableLinks); //<-- works $(this).unbind("click"); //<-- this does not work } }); } document.getElementById('st_select_element_button').addEventListener("click", highlightProcess);}
如何在綁定事件中解除綁定
開滿天機
2021-07-12 17:01:56