This means that there will be appropriate event bubbles and the browser will execute the assigned event handler. This ability is very useful when testing web applications. It provides a method to simulate specific events in the DOM level 3 specification. Both IE9 chrome FF Opera and Safari support this method. In IE8 and previous methods, IE browsers have their own way to simulate events in their own way.
a) Dom event simulation
You can create an event object at any time through the createEvent() method on the document. This method only accepts one parameter. It is necessary to create the event string of the event object. All strings are plural in the DOM2 level specification, and all strings are singular in the DOM level 3 event. All strings are as follows:
UIEvents: Common UI events, mouse events and keyboard events are inherited from UI events, and UIEvent is used at DOM level 3.
MouseEvents: A common mouse event, which is used on DOM level 3.
MutationEvents: A common mutation event, which is used at DOM level 3.
HTMLEvents: A common HTML event, not equivalent at DOM3 level.
Note that ie9 is the only browser that supports DOM3-level keyboard events, but other browsers also offer other methods available to simulate keyboard events.
Once an event object is created, the relevant information of the event must be initialized. Each type of event has a specific method to initialize. After creating the event object, the event is applied to a specific dom node through the dispatchEvent() method so that it can support the event. This dispatchEvent() event supports one parameter, which is the event object you created.
b) Mouse event simulation
Mouse event can be simulated by creating a mouse event object and granting it some relevant information. Create a mouse event by passing a string "MouseEvents" to create a mouse event object. Then, the returned event object is initialized through the iniMouseEvent() method. The iniMouseEvent() method accepts 15 parameters, and the parameters are as follows:
type string type: The type of event to be triggered, such as 'click'.
bubbles Boolean type: Indicates whether the event should bubble, for mouse event simulation, this value should be set to true.
cancelable bool type: indicates whether the event can be canceled. For mouse event simulation, the value should be set to true.
view abstract view: the view granted by the event, this value is almost all.
detail int type: Additional event information should generally default to 0 when initializing.
screenX int type: The X coordinate of the event to the left of the screen
screenY int type: the y coordinate of the event from the top of the screen
clientX int type: X coordinate to the left of the visible area
clientY int type: y coordinate above the event distance viewed area
ctrlKey Boolean type: represents whether the ctrol key is pressed, default is false.
altKey Boolean type: represents whether the alt key is pressed, default is false.
shiftKey Boolean type: represents whether the shift key is pressed, default is false.
metaKey Boolean type: represents whether the meta key is pressed, the default is false.
button int type: indicates the mouse button being pressed, default is zero.
relatedTarget (object): The associated object of the event. It is only used when mocking mouseover and mouseout.
It is worth noting that the parameters of initMouseEvent() are directly mapped to the event object. The first four parameters are used by the browser, and only the event processing function uses other parameters. When the event object is passed as a parameter to the dispatch() method, the target attribute will be automatically assigned a value. Here is an example,
var btn = ("myBtn");
var event = ("MouseEvents");
("click", true, true, , 0, 0, 0, 0, 0,false, false, false, false, 0, null);
(event);
In the DOM implemented browser, all other events include dbclick, which can be implemented in the same way.
c) Keyboard event simulation
It is worth noting that the keyboard event has been removed from the DOM2 level event. At first, in the draft version of the DOM2 level event, the keyboard event was part of the draft, but in the final version, FF has implemented the keyboard event in the draft version. It is worth noting that there are still great differences between the keyboard event implemented in the DOM3 level event and the keyboard event in the draft version of the DOM2 level event.
Create a keyboard event object in a dom3-level event. It is done by passing the KeyBoardEvent string as a parameter. For the returned event object, call the initKeyBoadEvent() method to initialize it. There are the following parameters for initializing the keyboard event:
type (string) - The event type to fire, such as "keydown".
bubbles (Boolean) — represents whether the event should bubble.
cancelable (Boolean) — represents whether the event can be cancelled.
view (AbstractView) — The event is granted is a graph. The usual value is:.
key (string) — the code corresponding to the key pressed.
location (integer) — where the key is pressed. 0: default keyboard, 1 left position, 2 right position, 3 numeric keyboard area, 4 virtual keyboard area, or 5 gamepad.
modifiers (string) — A list of modifiers separated by spaces.
repeat (integer) — The number of times a key is pressed in a row.
Please note that in the DOM3 event, the keypress event is lost, so as follows, you can only simulate the keydown and keyup events on the keyboard.
var textbox = ("myTextbox"),event;
if (("KeyboardEvents", "3.0")){
event = ("KeyboardEvent");
("keydown", true, true, , "a",0, "Shift", 0);
}
(event);
Under FF, you allow you to create keyboard events by using ('KeyEvents'), the initialization method is initKeyEvent(), which accepts 10 parameters.
type (string) — The type of event to fire, such as "keydown".
bubbles (Boolean) — represents whether the event should bubble.
cancelable (Boolean) — represents whether the event can be cancelled.
view (AbstractView) — The event is granted is a graph. The usual value is:.
ctrlKey (Boolean) — represents whether the ctrol key is pressed. Default false.
altKey (Boolean) — represents whether the alt key is pressed. Default false.
shiftKey (Boolean) — represents whether the shift key is pressed. Default false.
metaKey (Boolean) — means whether the meta key is pressed. Default false.
keyCode (integer) — The key code corresponding to the key when the key is pressed or released. The default is 0;
charCode (integer) — The ASCII code corresponding to the character of the pressed key is used by the total keypress event. The default is 0.
D) Simulate other events
Mouse events and keyboard events are the longest simulated events in the browser, but sometimes mutation events and HTML events are also required. You can use createEvent('MutationEvents') to create a mutation event object. You can use initMutationEvent() to initialize this event object. The parameters include type, bubbles, cancelable, relatedNode, prevValue,
newValue, attrName, and attrChange. The following methods can be used to simulate a mutation event:
var event = ('MutationEvents');
("DOMNodeInserted", true, false, someNode, "","","",0);
(event);
For HTML events, directly upload the code.
var event = ("HTMLEvents");
("focus", true, false);
(event);
Mutation events and HTML events are rarely used in browsers because of the limitations they collect on applications.
E) Customized DOM events
A type of event is defined in the DOM3 level event called custom event. I call it a customer event. The customer event will not be triggered by the dom natively, but is provided directly, so that developers can create their own event. You can create your own customer event. By calling createEvent('CustomEvent'), call the returned event object, initCustomEvent() method, which passes four parameters type, bubbles, cancelable, and detail. ps: I have limited understanding of this part, and here I am just a throwaway to attract jade.
F) Event simulation in IE
From IE8 and earlier versions of IE are imitating the way DOM simulates events: creating event objects, initializing event information, and then triggering events. Of course, the process of IE completing these steps is different.
First, unlike the method of creating event objects in dom, IE adopts the () method and does not have parameters, returning a common event object. Next, you have to assign a value to the returned event object. At this time, ie does not provide an initialization function. You can only use physical methods to assign values one by one, and finally call the fireEvent() method on the target element, with two parameters: the name of the event processing and the created event object. When the fireEvent method is called, the srcElement and type attributes of the event object will be automatically assigned, and other values will need to be assigned manually. Please see the following example:
var btn = ("myBtn");
var event = ();
= 100;
= 0;
= 0;
= 0;
= false;
= false;
= false;
= 0;
("onclick", event);
This example creates an event object, and then initializes the event object with some information. Note that the assignment of event attributes is unordered. For event objects, these attribute values are not very important, because only the event handler corresponding to the event handle will use them. There is no difference between event objects that create mouse events, keyboard events, or other events, because a common event object can be triggered by any type of event.
It is worth noting that in Dom's keyboard event simulation, the result of a keypress simulation event will not appear as a character in the textbox, even if the corresponding event handling function has been triggered.
Compared with DOM event simulation, I personally think that IE event simulation is easier to remember and accept, and a unified event model can bring some convenience.
a) Dom event simulation
You can create an event object at any time through the createEvent() method on the document. This method only accepts one parameter. It is necessary to create the event string of the event object. All strings are plural in the DOM2 level specification, and all strings are singular in the DOM level 3 event. All strings are as follows:
UIEvents: Common UI events, mouse events and keyboard events are inherited from UI events, and UIEvent is used at DOM level 3.
MouseEvents: A common mouse event, which is used on DOM level 3.
MutationEvents: A common mutation event, which is used at DOM level 3.
HTMLEvents: A common HTML event, not equivalent at DOM3 level.
Note that ie9 is the only browser that supports DOM3-level keyboard events, but other browsers also offer other methods available to simulate keyboard events.
Once an event object is created, the relevant information of the event must be initialized. Each type of event has a specific method to initialize. After creating the event object, the event is applied to a specific dom node through the dispatchEvent() method so that it can support the event. This dispatchEvent() event supports one parameter, which is the event object you created.
b) Mouse event simulation
Mouse event can be simulated by creating a mouse event object and granting it some relevant information. Create a mouse event by passing a string "MouseEvents" to create a mouse event object. Then, the returned event object is initialized through the iniMouseEvent() method. The iniMouseEvent() method accepts 15 parameters, and the parameters are as follows:
type string type: The type of event to be triggered, such as 'click'.
bubbles Boolean type: Indicates whether the event should bubble, for mouse event simulation, this value should be set to true.
cancelable bool type: indicates whether the event can be canceled. For mouse event simulation, the value should be set to true.
view abstract view: the view granted by the event, this value is almost all.
detail int type: Additional event information should generally default to 0 when initializing.
screenX int type: The X coordinate of the event to the left of the screen
screenY int type: the y coordinate of the event from the top of the screen
clientX int type: X coordinate to the left of the visible area
clientY int type: y coordinate above the event distance viewed area
ctrlKey Boolean type: represents whether the ctrol key is pressed, default is false.
altKey Boolean type: represents whether the alt key is pressed, default is false.
shiftKey Boolean type: represents whether the shift key is pressed, default is false.
metaKey Boolean type: represents whether the meta key is pressed, the default is false.
button int type: indicates the mouse button being pressed, default is zero.
relatedTarget (object): The associated object of the event. It is only used when mocking mouseover and mouseout.
It is worth noting that the parameters of initMouseEvent() are directly mapped to the event object. The first four parameters are used by the browser, and only the event processing function uses other parameters. When the event object is passed as a parameter to the dispatch() method, the target attribute will be automatically assigned a value. Here is an example,
Copy the codeThe code is as follows:
var btn = ("myBtn");
var event = ("MouseEvents");
("click", true, true, , 0, 0, 0, 0, 0,false, false, false, false, 0, null);
(event);
In the DOM implemented browser, all other events include dbclick, which can be implemented in the same way.
c) Keyboard event simulation
It is worth noting that the keyboard event has been removed from the DOM2 level event. At first, in the draft version of the DOM2 level event, the keyboard event was part of the draft, but in the final version, FF has implemented the keyboard event in the draft version. It is worth noting that there are still great differences between the keyboard event implemented in the DOM3 level event and the keyboard event in the draft version of the DOM2 level event.
Create a keyboard event object in a dom3-level event. It is done by passing the KeyBoardEvent string as a parameter. For the returned event object, call the initKeyBoadEvent() method to initialize it. There are the following parameters for initializing the keyboard event:
type (string) - The event type to fire, such as "keydown".
bubbles (Boolean) — represents whether the event should bubble.
cancelable (Boolean) — represents whether the event can be cancelled.
view (AbstractView) — The event is granted is a graph. The usual value is:.
key (string) — the code corresponding to the key pressed.
location (integer) — where the key is pressed. 0: default keyboard, 1 left position, 2 right position, 3 numeric keyboard area, 4 virtual keyboard area, or 5 gamepad.
modifiers (string) — A list of modifiers separated by spaces.
repeat (integer) — The number of times a key is pressed in a row.
Please note that in the DOM3 event, the keypress event is lost, so as follows, you can only simulate the keydown and keyup events on the keyboard.
Copy the codeThe code is as follows:
var textbox = ("myTextbox"),event;
if (("KeyboardEvents", "3.0")){
event = ("KeyboardEvent");
("keydown", true, true, , "a",0, "Shift", 0);
}
(event);
Under FF, you allow you to create keyboard events by using ('KeyEvents'), the initialization method is initKeyEvent(), which accepts 10 parameters.
type (string) — The type of event to fire, such as "keydown".
bubbles (Boolean) — represents whether the event should bubble.
cancelable (Boolean) — represents whether the event can be cancelled.
view (AbstractView) — The event is granted is a graph. The usual value is:.
ctrlKey (Boolean) — represents whether the ctrol key is pressed. Default false.
altKey (Boolean) — represents whether the alt key is pressed. Default false.
shiftKey (Boolean) — represents whether the shift key is pressed. Default false.
metaKey (Boolean) — means whether the meta key is pressed. Default false.
keyCode (integer) — The key code corresponding to the key when the key is pressed or released. The default is 0;
charCode (integer) — The ASCII code corresponding to the character of the pressed key is used by the total keypress event. The default is 0.
D) Simulate other events
Mouse events and keyboard events are the longest simulated events in the browser, but sometimes mutation events and HTML events are also required. You can use createEvent('MutationEvents') to create a mutation event object. You can use initMutationEvent() to initialize this event object. The parameters include type, bubbles, cancelable, relatedNode, prevValue,
newValue, attrName, and attrChange. The following methods can be used to simulate a mutation event:
var event = ('MutationEvents');
("DOMNodeInserted", true, false, someNode, "","","",0);
(event);
For HTML events, directly upload the code.
var event = ("HTMLEvents");
("focus", true, false);
(event);
Mutation events and HTML events are rarely used in browsers because of the limitations they collect on applications.
E) Customized DOM events
A type of event is defined in the DOM3 level event called custom event. I call it a customer event. The customer event will not be triggered by the dom natively, but is provided directly, so that developers can create their own event. You can create your own customer event. By calling createEvent('CustomEvent'), call the returned event object, initCustomEvent() method, which passes four parameters type, bubbles, cancelable, and detail. ps: I have limited understanding of this part, and here I am just a throwaway to attract jade.
F) Event simulation in IE
From IE8 and earlier versions of IE are imitating the way DOM simulates events: creating event objects, initializing event information, and then triggering events. Of course, the process of IE completing these steps is different.
First, unlike the method of creating event objects in dom, IE adopts the () method and does not have parameters, returning a common event object. Next, you have to assign a value to the returned event object. At this time, ie does not provide an initialization function. You can only use physical methods to assign values one by one, and finally call the fireEvent() method on the target element, with two parameters: the name of the event processing and the created event object. When the fireEvent method is called, the srcElement and type attributes of the event object will be automatically assigned, and other values will need to be assigned manually. Please see the following example:
Copy the codeThe code is as follows:
var btn = ("myBtn");
var event = ();
= 100;
= 0;
= 0;
= 0;
= false;
= false;
= false;
= 0;
("onclick", event);
This example creates an event object, and then initializes the event object with some information. Note that the assignment of event attributes is unordered. For event objects, these attribute values are not very important, because only the event handler corresponding to the event handle will use them. There is no difference between event objects that create mouse events, keyboard events, or other events, because a common event object can be triggered by any type of event.
It is worth noting that in Dom's keyboard event simulation, the result of a keypress simulation event will not appear as a character in the textbox, even if the corresponding event handling function has been triggered.
Compared with DOM event simulation, I personally think that IE event simulation is easier to remember and accept, and a unified event model can bring some convenience.