簡體   English   中英

如何刪除頁面消息?

[英]How remove page message?

我正在嘗試從頁面中刪除一條消息,但只有在顯示此消息時我才能執行我的代碼。 我怎樣才能做到這一點? 由事件偵聽器刪除是否更合適?

function 是這樣的:

function removeWindow(){
  document.querySelector(".content_form").remove();
  document.querySelector(".pop").style.background = 'transparent';
  document.querySelector(".footer").remove();
}

提前致謝!

如果您正在尋找一種在這些掛載之前“監視”DOM 的方法,您需要使用MutationObserver

但是你可以通過添加一行 CSS 來更容易地做到這一點。

.pop { display: none; }

即使它必須寫在 JavaScript 中,也只需在 JavaScript 中添加 CSS 規則,不要擔心時間問題。

您可以使用window.setInterval不斷檢查元素是否存在,直到它出現並刪除它:

function removeWindow() {
  const contentForm = document.querySelector(".content_form");

  console.log('Checking...');

  if (!contentForm) return;

  contentForm.remove();
  document.querySelector(".pop").style.background = 'transparent';
  document.querySelector(".footer").remove();

  // Stop polling as we have already removed it:
  clearInterval(intervalID);

  console.log('GONE!');
}

// Start polling, once every second:
const intervalID = setInterval(removeWindow, 1000);

您可以在“pop”容器上設置重要樣式,以防止出現在屏幕上。 由於 js 在頁面加載之前無法找到元素,因此 CSS 將始終有效。 希望這對你有幫助。

  • styles:
.PD-V2.anonymous .register_file .pop{
 top: 100% !important
}

暫無
暫無

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

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