簡體   English   中英

如何通過突變觀察者獲取觀察到的元素

[英]How to get observed element by a mutation observer

你好,

我有一個觀察者,我這樣稱呼它:

myobserver.observe(mydiv, {childList : true, subtree : true, attributes : true});

我需要獲取觀察到的元素,像這樣

myobserver = new MutationObserver((mymutations) => {
 console.log(observed);  //should output "mydiv" always regardless of the mutation
});

我嘗試了 mymutations.target 但這不會,因為如果突變發生在其中一個孩子身上,它會給我帶來不同的元素。 有什么辦法嗎?

謝謝

也許是這樣的:

function observeElement(element) {
  const observed = element;

  const myobserver = new MutationObserver((mymutations) => {
    console.log(observed);
  });

  myobserver.observe(observed, {
    childList: true,
    subtree: true,
    attributes: true
  });
}

observeElement(myDiv);

暫無
暫無

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

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