簡體   English   中英

JavaScript 突變觀測器

[英]JavaScript Mutation observer

我正在嘗試使用 Mutation Observer。 我使用配置:

config = { characterData: true, characterDataOldValue: true };

我的 html 頁面:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script src="script.js"></script>
        <title>Test</title>
</head>
<body>
    <p id="test" contenteditable="true">Test<b>subtree</b>test</p>

在我將子樹放入配置之前,突變觀察者無法工作:

config = { characterData: true, subtree: true, characterDataOldValue: true };

誰能解釋為什么? 我不明白。

下面是變異觀察者的代碼,它工作正常。 您能否顯示更多代碼,以便找出問題所在。 有關更多信息,您必須參考這個DOM MutationObserver - 在不影響瀏覽器性能的情況下對 DOM 更改做出反應。

 const targetNode = document.getElementById('test'); console.log(targetNode); // Create an observer instance linked to the callback function const observer = new MutationObserver((mutationList,observer)=>{ console.log("working"); }); // Options for the observer (which mutations to observe) const config = { attributes: true, childList: true, subtree: true }; // Start observing the target node for configured mutations observer.observe(targetNode, config); // Later, you can stop observing //observer.disconnect();
 <p id="test" contenteditable="true">Test</p>

暫無
暫無

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

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