gpt4 book ai didi

Javascript - 对象不支持 document.createEvent

转载 作者:行者123 更新时间:2023-12-03 00:58:27 25 4
gpt4 key购买 nike

我正在开发一个自动化项目,我试图单击网站上的按钮。我已设法识别该元素,但当我尝试使用 document.createEvent 时,它告诉我该对象不支持它。有什么想法吗?

function categoryClick() {
var events = ["mousemove", "mouseover", "focus", "mousedown", "mouseup", "click"];
var combobox = document.getElementById("category");
var a = combobox.childNodes;
var target = a[0].childNodes[0];

for (var i = 0; i < events.length; i++) {
var eventObject = document.createEvent("Event");
eventObject.initEvent(events[i], true, false);

target.dispatchEvent(eventObject);
}
}

最佳答案

createEvent 已贬值

参见:https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent

它已被替换为Event()

参见:https://developer.mozilla.org/en-US/docs/Web/API/Event/Event

因此,对于您的代码,这应该作为重构:

function categoryClick() {
var events = ["mousemove", "mouseover", "focus", "mousedown", "mouseup", "click"];
var combobox = document.getElementById("category");
var a = combobox.childNodes;
var target = a[0].childNodes[0];

for (var i = 0; i < events.length; i++) {
var eventObject = new Event(events[i], {"bubbles":true, "cancelable":false});

target.dispatchEvent(eventObject);
}
}

关于Javascript - 对象不支持 document.createEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52720939/

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