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

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

WBOY
Release: 2016-05-16 17:09:19
Original
1359 people have browsed it

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');
}
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template