MutationObserver - getting "TypeError: MutationObserver.observe: Argument 1 does not implement interface Node."
MutationObserver - getting "TypeError: MutationObserver.observe: Argument 1 does not implement interface Node."
我正在尝试检测由图像滑块插件动态设置的元素的高度,并使用它来设置容器的高度。
获取“类型错误:MutationObserver.observe:参数 1 未实现接口节点。”
我检查了MutationObserver documentation and its options。看到了
At a minimum, one of childList, attributes, and/or characterData must be true when you call observe(). Otherwise, a TypeError exception will be thrown.
并且我将属性设置为 true 但仍然收到 typeError
jQuery(document).ready(function($){
// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
for(const mutation of mutationsList) {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
};
const observer = new MutationObserver(callback);
//set up your configuration
const config = { attributes:true, subtree: false };
var changingContainer = $('.soliloquy-viewport');
//start observing
observer.observe(changingContainer, config);
//change height on button press
function changeHeight(){
changingContainer.height(Math.floor((Math.random() * 100) + 20));
}
$("#height").click(changeHeight);
});
.soliloquy-viewport{
background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="soliloquy-viewport">
Hello
</div>
<button id="height">
change height
</button>
MutationObervers 仅适用于 Element 对象,不适用于 jQuery 对象。使用 get()
将 observe()
的第一个参数更改为基础元素,如下所示:
observer.observe(changingContainer.get(0), config);
或者像这样按索引访问 jQuery 对象:
observer.observe(changingContainer[0], config);
jQuery(document).ready(function($) {
let $changingContainer = $('.soliloquy-viewport');
const observer = new MutationObserver((ml, o) => {
for (const m of ml) {
console.log('The ' + m.attributeName + ' attribute was modified.');
}
});
observer.observe($changingContainer.get(0), {
attributes: true,
subtree: false
});
//change height on button press
$("#height").click(() => $changingContainer.height(Math.floor((Math.random() * 100) + 20)));
});
.soliloquy-viewport {
background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="soliloquy-viewport">Hello</div>
<button id="height">change height</button>
请注意,这仅适用于单个元素。对于具有相同 class 的多个元素,您将需要遍历它们并单独应用 MO。
我正在尝试检测由图像滑块插件动态设置的元素的高度,并使用它来设置容器的高度。
获取“类型错误:MutationObserver.observe:参数 1 未实现接口节点。”
我检查了MutationObserver documentation and its options。看到了
At a minimum, one of childList, attributes, and/or characterData must be true when you call observe(). Otherwise, a TypeError exception will be thrown.
并且我将属性设置为 true 但仍然收到 typeError
jQuery(document).ready(function($){
// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
for(const mutation of mutationsList) {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
};
const observer = new MutationObserver(callback);
//set up your configuration
const config = { attributes:true, subtree: false };
var changingContainer = $('.soliloquy-viewport');
//start observing
observer.observe(changingContainer, config);
//change height on button press
function changeHeight(){
changingContainer.height(Math.floor((Math.random() * 100) + 20));
}
$("#height").click(changeHeight);
});
.soliloquy-viewport{
background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="soliloquy-viewport">
Hello
</div>
<button id="height">
change height
</button>
MutationObervers 仅适用于 Element 对象,不适用于 jQuery 对象。使用 get()
将 observe()
的第一个参数更改为基础元素,如下所示:
observer.observe(changingContainer.get(0), config);
或者像这样按索引访问 jQuery 对象:
observer.observe(changingContainer[0], config);
jQuery(document).ready(function($) {
let $changingContainer = $('.soliloquy-viewport');
const observer = new MutationObserver((ml, o) => {
for (const m of ml) {
console.log('The ' + m.attributeName + ' attribute was modified.');
}
});
observer.observe($changingContainer.get(0), {
attributes: true,
subtree: false
});
//change height on button press
$("#height").click(() => $changingContainer.height(Math.floor((Math.random() * 100) + 20)));
});
.soliloquy-viewport {
background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="soliloquy-viewport">Hello</div>
<button id="height">change height</button>
请注意,这仅适用于单个元素。对于具有相同 class 的多个元素,您将需要遍历它们并单独应用 MO。