jquery - 使用 MutationObserver 检测类更改?
问题描述
我有一个<body>
电话<body id="check-in-body">
,我想检测课程何时更改。它可以从class="theme-1"
to改变class="theme-2"
等等。
我有下面的代码,但我不能让它工作。在从下面的代码中删除&& (mutation.attributeName === "class")
和删除时,我能够使其部分工作。attributeFilter: ['class']
但这一直触发,因为当我有一个可见的模态时style="overflow: hidden;"
,body
我认为这就是触发它的原因。我如何只听班级变化body
?
function searchBackgroundFetch() {
var element = document.querySelector('#check-in-body');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if ((mutation.type === "attributes") && (mutation.attributeName === "class")) {
console.log('Yeah');
}
});
});
observer.observe(element, {
attributes: true,
attributeFilter: ['class']
});
}
解决方案
推荐阅读
- javascript - js 按值从对象数组中删除重复项并计算重复项
- c# - 在统一中,如何访问作为对象子级的按钮文本?
- amazon-web-services - 如何在 Fargate 上的 AWS ECS 环境中存储 GOOGLE_APPLICATION_CREDENTIALS?
- python - 在 split 命令中应用 like 运算符以过滤数据框中的数据
- python - cloudinary.exceptions.Error:使用 Flask 的空文件
- android - 如何解决“发生意外错误。请在 Google Play 商店应用程序发布中重试 (7515006A)'
- mysql - 传递给 ransacker 的 Active Admin 过滤器参数
- javascript - 在数组对象内映射数组
- python - os.path.join('BASE_DIR','template') 问题
- hibernate - 当我试图将我们的整个数据移动到新服务器时,我们面临着这个问题