Home  >  Article  >  Web Front-end  >  Summary of javascript active dispatch events_javascript skills

Summary of javascript active dispatch events_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:03:261264browse

1. dispatchEvent()
This is the standard triggering event method. When using it, you need to create the event object first. As follows

Copy code The code is as follows:

var evt = document.createEvent('Event') ;
evt.initEvent('click',true,true);
element.dispatchEvent(evt);

2, fireEvent()
This is in the old version of IE The implemented trigger event method can be used directly without creating an event object. As follows
Copy the code The code is as follows:

element.fireEvent('onclick');

Note: Like attachEvent, it needs to be added with "on"
A version compatible with all browsers, as follows
Copy code The code is as follows:

var dispatch = window.addEventListener ?
function(el, type){
try{
var evt = document. createEvent('Event');
evt.initEvent(type,true,true);
el.dispatchEvent(evt);
}catch(e){alert(e)};
} :
function(el, type){
try{
el.fireEvent('on' type);
}catch(e){alert(e)}
};

The above encapsulates a general method for triggering events, which can trigger various events. Browsers also provide specific methods to trigger individual events.
4. click()
is used to simulate user clicks. Except for Safari/Chrome which does not support non-input/button, other browsers support it.
5, form.submit()
Simulate form submission, relative to clicking input[type=submit]
6, input/textarea.select()
Simulate user-selected text.
7, focus()
Simulate getting cursor focus
8, blur()
Simulate losing cursor focus
9, input/textarea/select.change()
Simulate text or The options change
10. The dispatch of custom events is actually to get the function and then call

Related:
https://developer.mozilla.org/En/DOM/Element.dispatchEvent
http://msdn.microsoft.com/en-us /library/ms536423(v=VS.85).aspx
https://developer.mozilla.org/en/DOM/Input.select

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