Home  >  Article  >  Web Front-end  >  Code triggers js events (click, change) sample application_javascript skills

Code triggers js events (click, change) sample application_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:09:191333browse

If Chrome and Firefox do not support the fireEvent method
, you can use the dispatchEvent method instead and directly give a compatible Code.

Trigger click event

Copy Code The code is as follows:

function simulateClick(el) {
var evt;
if (document.createEvent) { // DOM Level 2 standard
evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(evt);
} else if (el.fireEvent) { // IE
el.fireEvent('onclick');
}
}

Trigger drag event
Copy code The code is as follows:

function simulateDrag(el) {
var evt;
if (document.createEvent) { // DOM Level 2 standard
evt = document.createEvent("MouseEvent");
evt.initMouseEvent ("dragstart", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(evt);
} else if (el.fireEvent) { // IE
el.fireEvent('ondragstart');
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn