【发布时间】:2018-05-31 23:32:10
【问题描述】:
嗨,
我需要 javascript 来点击一个 html 元素,但它没有立即出现在 dom 中,所以我有这个代码:
document.addEventListener("DOMContentLoaded", function(event) {
// Select the node that will be observed for mutations
var parentOfMyList = document.body;
// Options for the observer (which mutations to observe)
var config = {
attributes: true,
childList: true,
subtree: true
};
// Callback function to execute when mutations are observed
var callback = function(mutationsList) {
for (var mutation of mutationsList) {
if (mutation.type == 'childList') {
var elt = document.getElementById("topcmm-123flashchat-main-toolbar-message-type-option");
if (elt) {
setTimeout(document.getElementById("topcmm-123flashchat-main-toolbar-message-type-option").click, 6000);
observer.disconnect();
}
}
}
};
// Create an observer instance linked to the callback function
var observer = new MutationObserver(callback);
observer.observe(parentOfMyList, config);
});
但我收到一条错误消息,上面写着'click' called on an object that does not implement interface HTMLElement。这是为什么?当 click() 被执行时,该元素应该已经存在了(我什至给了它 6 秒的时间来赶上)。
谢谢。
【问题讨论】:
标签: javascript mutation-observers