MutationObserver 在新节点上循环
我在这里要做的是通过使它们成为“跨度”元素来突出显示一些文本节点。当新的消息/注释加载时,我也可以处理新添加的节点。但是,我发现MutationObserver不断找到新添加的节点。也许是因为dosothing
创建了新节点,并且它将由MutationObserver重新发现。我如何防止这种情况发生?不确定我是否应该将MutationObserver放入EventListener中,但它似乎并不是在它之外工作。
document.addEventListener('DOMContentLoaded', function() {
var allTextNodes = textNodesUnder(document.body);
doSomething(allTextNodes);
// Select the node that will be observed for mutations
const targetNode = document.body;
// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
function scanning(el) {
// el.style.visibility = "hidden";
var newTextNodes = textNodesUnder(el);
doSomething(newTextNodes);
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
//observer.disconnect(); //will not work if disconnect here
});
function doSomething(node) {
var newItem = document.createElement("SPAN");
newItem.style.backgroundColor = "red";
var textnode = document.createTextNode(node.textContent);
newItem.appendChild(textnode);
node.replaceWith(newItem);
}
What I'm trying to do here is to highlight some textnode by making them a "span" element. And I would it to work on newly added nodes as well when new message/comment loads. However, I found MutationObserver keeps finding newly added node. Perhaps it is because doSomething
creates new node and it will be re-discovered by MutationObserver. How do I keep this from happening? Not sure if I should put MutationObserver inside EventListener but it doesnot seem to be working outside of it.
document.addEventListener('DOMContentLoaded', function() {
var allTextNodes = textNodesUnder(document.body);
doSomething(allTextNodes);
// Select the node that will be observed for mutations
const targetNode = document.body;
// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
function scanning(el) {
// el.style.visibility = "hidden";
var newTextNodes = textNodesUnder(el);
doSomething(newTextNodes);
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
//observer.disconnect(); //will not work if disconnect here
});
function doSomething(node) {
var newItem = document.createElement("SPAN");
newItem.style.backgroundColor = "red";
var textnode = document.createTextNode(node.textContent);
newItem.appendChild(textnode);
node.replaceWith(newItem);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次更改节点的内容时,您的
MutationObserver
回调都会被调用,包括您在回调本身中执行此操作的时间。回调中发生更改,请调用observer.disconnect()
,然后在进行更改后使用observe
重新连接:Your
MutationObserver
callback gets called every time you change the contents of the node, including the times that you do so within the callback itself. Before making changes within the callback, callobserver.disconnect()
, then reconnect usingobserve
after making your changes: