javascript - JavaScript 变异观察者
问题描述
我正在尝试使用 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>
推荐阅读
- r - 如何将数据框中的数据点添加到斯洛伐克行政区的多边形地图?
- c - 一次打印后终止进程的 Unix 命令行参数
- java - 在两个正在运行的活动之间传递数据
- java - 将多个名称保存在字符串数组中
- scala - Scala - 如何使用来自另一个类的数据初始化对象
- ansible - 将oracle输出存储在ansible中并检查oracle表中列输出的条件
- sql-server - 在视图上使用 sp_helptext 时出错
- vue.js - 从 vue-loader 缓存中删除已删除的组件
- javascript - 如何修复 InvariantViolation?
- sql - 在 graphQL 服务器中使用 SQL datasarouce 实现嵌套游标分页的最佳方法是什么?