A brief discussion on Javascript event simulation_javascript skills
This means that the appropriate events will bubble up and the browser will execute the assigned event handlers. This ability is very useful when testing web applications. The DOM level 3 specification provides methods to simulate specific events. IE9 chrome FF Opera and Safari support this method. In IE8 and previous methods, IE browser has its own way to simulate events
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, which requires creating an event. The event string of the object. In the DOM2 level specification, all strings are in plural form. In DOM level 3 events, all strings are in singular form. All strings are as follows:
UIEvents: general UI events , Mouse events and keyboard events are all inherited from UI events, and UIEvent is used at DOM level 3.
MouseEvents: Universal mouse events, MouseEvent is used on DOM level 3.
MutationEvents: Universal mutation event, MutationEvent is used at DOM level 3.
HTMLEvents: Universal HTML events, there is no equivalent at DOM3 level.
Note that IE9 is the only browser that supports DOM3 level keyboard events, but other browsers also provide other available methods 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 the event object is created, the event is applied to the event through the dispatchEvent() method. On a specific dom node so that it supports this event. This dispatchEvent() event supports one parameter, which is the event object you created.
b) Mouse event simulation
Mouse events can be simulated by creating a mouse event object (mouse event object) and granting it some relevant information. Create a mouse event by passing a string to the createEvent() method" MouseEvents" to create a mouse event object, and then initialize the returned event object through the iniMouseEvent() method. The iniMouseEvent() method accepts 15 parameters. 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, this value should be set to true.
View abstract view: The view granted by the event. This value is almost always document.defaultView.
Detail int type: Additional event information. This should generally default to 0 during initialization.
ScreenX int type: The X coordinate from the left side of the screen from the event
screenY int type: The y coordinate from the top of the screen from the event
clientX int type: The X coordinate from the left side of the visible area from the event
clientY int type : The y coordinate of the event distance above the visible area
ctrlKey Boolean type: Represents whether the ctrol key is pressed, the default is false.
altKey Boolean type: represents whether the alt key is pressed, the default is false.
shiftKey Boolean type: represents whether the shift key is pressed, the default is false.
MetaKey Boolean type: Represents whether the meta key is pressed. The default is false.
Button int type: Represents the pressed mouse button, the default is zero.
relatedTarget (object): The related object of the event. Only used when simulating 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. Only the event processing function uses other parameters. When the event object is passed as a parameter For the dispatch() method, the target attribute will be automatically assigned a value. The following is an example,
var btn = document.getElementById( "myBtn");
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, document.defaultView, 0, 0, 0, 0, 0,false , false, false, false, 0, null);
btn.dispatchEvent(event);
In DOM-implemented browsers, all other events, including dbclick, can be implemented in the same way.
c) Keyboard event simulation
It is worth noting that keyboard events have been moved out of DOM2-level events. Initially, in the draft version of DOM2-level events, keyboard events were included as part of the draft, but in the final version they were Moved out, FF has implemented the keyboard events in the draft version. It is worth noting that the keyboard events implemented in DOM3 level events are still very different from the keyboard events in the DOM2 level event draft version.
Creating a keyboard event object in a dom3 level event is through the createEvent() method and passing in the KeyBoardEvent string as a parameter. For the returned event object, call the initKeyBoadEvent() method to initialize it. The parameters for initializing the keyboard event are as follows: Each:
Type (string) - The type of event to be triggered, such as "keydown".
Bubbles (Boolean) — Represents whether the event should bubble.
Cancelable (Boolean) — Represents whether the event can be canceled .
view (AbstractView) — The event is granted to the image. Usually the value is: document.defaultView.
key (string) — The code corresponding to the pressed key.
location (integer) — Pressed The position of the key. 0: Default keyboard, 1 left position, 2 right position, 3 numeric keyboard area, 4 virtual keyboard area, or 5 game controller.
modifiers (string) — A space-separated modifier List.
Repeat (integer) — The number of times a key is pressed in a row.
Please note that in the DOM3 event, the keypress event is wasted, so in the following way, you can only simulate keydown and keyup events on the keyboard.
var textbox = document.getElementById("myTextbox"), event;
if (document.implementation.hasFeature("KeyboardEvents", "3.0")){
event = document.createEvent("KeyboardEvent");
event.initKeyboardEvent("keydown", true, true, document.defaultView, "a",0, "Shift", 0);
}
textbox.dispatchEvent(event);
Under FF, you are allowed to create keyboard events by using document.createEvent('KeyEvents'). The initialization method is initKeyEvent(). This method accepts 10 parameters,
type (string) — The type of event to be triggered, such as "keydown".
bubbles (Boolean) — Represents whether the event should bubble.
Cancelable (Boolean) — Represents whether the event can be canceled.
view (AbstractView) — The event is granted to the picture. The usual value is: document.defaultView.
ctrlKey (Boolean) — represents whether the ctrol key is pressed. The default is false.
altKey (Boolean) — represents whether the alt key is pressed. The default is false. .
shiftKey (Boolean) — represents whether the shift key is pressed. Default is false.
metaKey (Boolean) — represents whether the meta key is pressed. Default is false.
keyCode (integer) — key is pressed or released. The key code corresponding to the key. The default is 0;
CharCode (integer) — The ASCII code corresponding to the character of the pressed key. The default used by the keypress event is 0.
D) Simulate other events
Mouse events and keyboard events are the most commonly simulated events in browsers, but sometimes mutation events and HTML events also need to be simulated. 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. Yes Use the following method to simulate a mutation event:
var event = document.createEvent('MutationEvents');
event.initMutationEvent("DOMNodeInserted", true, false, someNode, "","","" ,0);
Target.dispatchEvent(event);
For HTML events, enter the code directly.
var event = document.createEvent("HTMLEvents");
event.initEvent("focus", true, false);
target.dispatchEvent(event);
For mutation events and HTML events are rarely used in browsers because they impose application restrictions.
E) Customized DOM events
In DOM3 level events, a type of event is defined called custom event. I call it customer event. Customer event will not be triggered by DOM natively, but will be provided directly. As for developers who can create their own events, you can create your own customer event by calling createEvent('CustomEvent'), calling the initCustomEvent() method on the returned event object, passing four parameters: type, bubbles, cancelable , detail. ps: My understanding of this part is limited, so I am just giving some advice here.
F) Event simulation in IE
From IE8, and earlier versions of IE, they imitate the way DOM simulates events: creating event objects, initializing event information, and then triggering events. Of course, IE's process of completing these steps is different.
First of all, different from the method of creating event objects in DOM, IE uses the document.createEventObject() method and has no parameters. It returns a general event object. Next, you need to assign a value to the returned event object. At this time, IE does not provide For the 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 handler 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 the others will need to be assigned manually. Please see the following example:
var btn = document.getElementById ("myBtn");
var event = document.createEventObject();
event.screenX = 100;
event.screenY = 0;
event.clientX = 0;
event. clientY = 0;
event.ctrlKey = false;
event.altKey = false;
event.shiftKey = false;
event.button = 0;
btn.fireEvent("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 handler function corresponding to the event handler will use them. There is no difference between creating an event object for mouse events, keyboard events, or other events, because a generic 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 characters in the textbox, even if the corresponding event handler has been triggered.
Compared with DOM event simulation, I personally feel that IE's event simulation is easier to remember and accept. A unified event model can bring some convenience.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a
