gpt4 book ai didi

Javascript:mutationobserver 没有警告消息

转载 作者:行者123 更新时间:2023-12-03 21:15:37 25 4
gpt4 key购买 nike

我正在尝试检测元素中的 CSS 属性更改。我在网上搜索发现MutationObserver javascript API。但在我的测试脚本中,它没有按预期工作(它没有提醒属性名称和属性值)。

var foo = document.getElementById("hideit");

var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
alert('mutation.type = ' + mutation.type);

});

});
observer.observe(foo);
observer.disconnect();

$(function() {
$("#clickhere").on("click", function() {
$("#hideit").slideToggle('fase');
});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<body>
<div id="clickhere">click to toggel</div>
<div id="hideit" style="display:none;">this is the content of the hide/show toggle</div>

</body>


它显示一个javascript错误
TypeError: Argument 1 of MutationObserver.observe is not an object.

谢谢是提前

最佳答案

您的代码中有两个问题:

  • observer.observe 的使用是不正确的。它需要 2 个参数:节点和 MutationObserverInit .查看正确的 API here .
  • 请勿调用observer.disconnect();紧接在 observe 之后. Disconnect停止观察。

  • Working example

    关于Javascript:mutationobserver 没有警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36172983/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com