- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个第 3 方脚本,可以在我的页面上加载一个图片库,其中包含来自场外的图像。
我的页面开始时是空的:
<div class="container-fluid" id="cincopa">
</div>
然后第 3 方脚本会添加其他内容(例如照片库的框架):
<div class="container-fluid" id="cincopa">
<div id="cp_widget_38cc1320-f4a4-407a-a80e-1747bd339b64">
</div>
</div>
最后加载图像:
<div class="container-fluid" id="cincopa">
<div id="cp_widget_38cc1320-f4a4-407a-a80e-1747bd339b64">
<div class="galleria_images">
<div class="galleria_image">SomeImage</div>
<div class="galleria_image">SomeImage</div>
<div class="galleria_image">SomeImage</div>
</div>
</div>
</div>
我想:
显示加载动画
在 $('#cincopa')
MutationObserver
当它检测到 $('.galleria_image')
已创建时,这意味着图像已加载,因此我可以
移除加载动画
代码:
var target = document.querySelector('#cincopa');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
console.log(mutations);
mutations.forEach(function(mutation) {
console.log(mutation.type);
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// start the observer, pass in the target node, as well as the observer options
observer.observe(target, config);
问题是 MutationObserver
仅控制台记录一个突变,而 MutationRecord
在其数组中只有一个突变。当第 3 方脚本创建 DOM 元素时,我预计会有很多变化。
我是否误解了 MutationObserver
的工作原理?
这是解决方案
// This is MeteorJS creating the loading spinning thing
var loadingView = Blaze.render(Template.loading, $('#cincopa')[0]);
// select the target node
var target = document.querySelector('#cincopa');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.target.className === "galleria_image"){
// a image has been loaded, so remove the loading spinner and
// kill the observer
Blaze.remove(loadingView);
observer.disconnect();
}
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true, subtree: true };
// start the observer, pass in the target node, as well as the observer options
observer.observe(target, config);
更新的解决方案
.forEach
是愚蠢的并且没有跳出循环的好方法,这意味着我正在向 Blaze.remove()
发送多个命令和 observer.disconnect()
,即使在找到 .galleria_image
之后也是如此。
所以我改用下划线
:
// create an observer instance
var observer = new MutationObserver(function(mutations) {
var loaded = _.find(mutations, function(mutation){
console.log("observer running");
return mutation.target.className === "galleria-image";
});
if(loaded){
Blaze.remove(loadingView);
observer.disconnect();
console.log("observer stopped");
};
});
最佳答案
有一个选项可以让您完全按照自己的意愿行事:观察元素的子树。只需添加 subtree: true
到 MutationObserver
的 config
。
// ...
// In this case case only these two are needed, I believe.
var config = {
childList: true,
subtree: true
};
// ...observe
这应该让您知道何时插入了 .gallaria_images
。作为旁注,您 (OP) 还应该仔细检查图像是否已加载。
关于javascript - MutationObserver 没有显示所有突变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36025159/
我不是很熟悉 JavaScript 和垃圾回收的内部结构。我想避免在使用 MutationObserver 时可能产生内存问题的循环引用,并且我知道如果 GC 假设错误,我可以使 MutationOb
fiddle :https://jsfiddle.net/7gj26hqu/ 我想要一个MutationObserver可以检测自身内部的所有新节点。在示例中,我设置了 {childList: tru
我正在尝试检测元素中的 CSS 属性更改。我在网上搜索发现MutationObserver javascript API。但在我的测试脚本中,它没有按预期工作(它没有提醒属性名称和属性值)。 var
所以我正在尝试更改一些 JavaScript 从此 content.forumobserver = content.build.forumobserver = function() { var
我正在尝试在我的插件中使用 MutationObserver。因此,我注入(inject)一个内容脚本,然后设置观察者。这似乎有效,而且检测到的突变似乎无法序列化为 JSON。但实际上我想使用this
在下面的代码中,通过重复单击 2 个按钮,观察者 仅在每个观察到的目标上的第一个更改时触发? (输出显示更改。) 如何让它在每次更改时都生效? var cc = 1; document.querySe
我正在使用 MutationObserver检测何时将特定类添加到元素。 const observer = new MutationObserver((mutations) => { muta
我试图从页面中获取特定的已删除元素属性,该属性可以有 0 - n 个副本并且可以动态添加。我开始使用 DOMNodeRemoved 事件,最初了解到该事件已被弃用,然后转向 MutationObser
我正在尝试使用 Greasemonkey 为 Facebook 的时间线日志页面制作一个脚本。 我需要捕获两种事件: 1) URL 更改(由于更改是通过 Facebook 上的 AJAX 进行的,因此
以下代码允许我在单个节点上监听类更改: var target = $(".right-border")[0] var observer = new MutationObserver(function(
我想同步一些DOM节点属性。如果更改某个属性,则会更改其他元素属性。 我可以更改它,但我无法为其编写测试。该测试更改观察到的元素的属性,然后检查更改是否应用于其他元素。更改同步最终会发生,但不会在观察
对我来说,这个 API 中最令人困惑的是为什么使用它。我了解 ReactJS 和 RxJS,并且我已经习惯了 View 对数据更改使用react的概念。因此,观察 DOM 的变化(这肯定是在数据发生一
是否可以通过mutationObserver观察节点的innerHTML?我正在尝试使用 characterData: true,但它不会对内容更改进行任何回调。 完整选项列表: childList:
我试图等到 Mutation 事件开始覆盖它刚收到的值。在我尝试正常应用它之前,它一直被我无法控制的第 3 方提供的其他值覆盖。 /** Intercept changes in select2-dr
我打算使用 MutationObserver 来观察元素值的外观和变化,但老实说我不确定应该如何实现。 MO 的目标是 div.player-bar 而我想要完成的是检测 el-badge__cont
我在 chrome 扩展中有以下代码: var observer = new MutationObserver(function (mutations) { mutations.forEach(fun
我正在尝试使用 MutationObserver 来观察 body 元素中样式属性的变化,并将 body 样式属性的溢出属性更改为滚动。当我缩小浏览器窗口时,样式属性会发生变化。Firefox 最终崩
我有一个第 3 方脚本,可以在我的页面上加载一个图片库,其中包含来自场外的图像。 我的页面开始时是空的: 然后第 3 方脚本会添加其他内容(例如照片库的框架):
我正在使用 MutationObserver 来保存可拖动对象中的位置变化。 看起来像这样: let observer = new MutationObserver( (mutations) =>
以下脚本有效,但 chrome 显示有错误.. 我应该怎么做才能解决这个错误? function Do(){alert("test");} new MutationObserver(Do).obser
我是一名优秀的程序员,十分优秀!