gpt4 book ai didi

javascript - 特定元素是否存在事件

转载 作者:行者123 更新时间:2023-11-30 09:26:03 25 4
gpt4 key购买 nike

是否有任何事件可以知道原始 javascript 中的特定元素何时“开始存在”?比如我有

<div class="parent">
<div class="child"></div>
</div>

我想在 .parent 并且只有 .parent(不是 .child)“开始存在”时使用事件而不是将 js 代码放入其中。我尝试 setInterval 并检查 .parent 是否存在。

最佳答案

此功能,使用 DOMSubtreeModified 等的突变事件,现在已完全删除。 as shown here , 但您可以改用 MutationObserver specs here

关于如何使用它的一个简单示例取自 here这个:

// select the target node
var target = document.querySelector('#some-id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation.type);
});
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true }

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
observer.disconnect();

您可以在这里获取观察者配置对象的所有配置参数MutationObserverInit

关于javascript - 特定元素是否存在事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49069780/

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