我正在尝试在Internet Explorer中的元素上触发一系列事件,但以下代码给了我一个"事件未定义"。 任何想法可以做些什么来解决这个问题? 对象不支持 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 = new Event(events[i], {
"bubbles": true,
"cancelable": false
});
target.dispatchEvent(eventObject);
}
}
请参阅代码中的注释。
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++) {
if (typeof(Event) === 'function') { // just as you did
var eventObject = new Event(events[i], {
"bubbles": true,
"cancelable": false
})
} else { // fallback for IE
var eventObject = document.createEvent('Event');
eventObject.initEvent(events[i], true, false);
}
target.dispatchEvent(eventObject);
}
}