This article mainly introduces the use of js to implement event processing models. It has certain reference value for beginners. Interested friends can refer to it.
There are four event models in various browsers: original event model, standard event model, IE event model, and a Netscape4 event model, which will be introduced in detail below.
1. There are currently four event processing models: original event model, standard event model, IE event model, and a Netscape4 event model, but it can basically be ignored
2. The event processing model can be divided into two types: basic event processing and advanced event processing. The original event model belongs to basic event processing, and the standard event model and IE event model belong to advanced event processing
1. Basic event processing :
Basic event processing mainly refers to the event processing implemented by the original event model. It is mainly divided into the following two types:
(1) Event processing as an HTML tag, such as
…… p> //Here, onmouseover is just a representative, and also includes many other events
In this method, JS code strings are assigned to onmouseover and other event processing functions, and the system will automatically package these code strings in an anonymous function. There can be the this keyword, which points to the tag element, and the event keyword, which represents the event object when the event occurs (used in standard browsers). Such as
……
In fact, we can regard onmouseover, etc. as a function. Before assigning a value to it, it It's just an empty function. After assigning js code to it, it is equivalent to adding code to the empty function. Because onmouseover, etc. are actually a function, we can call it explicitly, such as element.onmouseover(), but this will not cause the mouseover event to actually occur.
You can return false to the event function (i.e. onmoouseover, etc.) to cause the default action to occur.
The function runs in the scope in which it is defined, so if you assign js code to the event processing function, it is equivalent to defining a function in this HTML tag environment. This environment is quite special, and its high The first-level scope is not the window global object environment. There is at least one scope environment between the two. As for the function defined in <script>, its higher-level scope environment is window (of course, nested functions are another matter). Therefore, in HTML tags, it is best to put the code in a function defined in <script>, and then assign this function call to the event function, that is, <p onmouseover="function();"> ……</script>
(2), event processing as javascript attribute For example, element.onmouseover=function(){……}
Note that in this way, you can no longer give The event handling function is assigned a js code string, but directly assigns the function (not a function call) to it, or assigns an anonymous function, such as element.onmouseover=function(){……} or element.onmouseover=f; f As a function, parentheses cannot be added here
Basic event processing will also propagate upward in the form of bubbling
2. Advanced event processing:
Advanced event processing only refers to event processing implemented by the standard event model and IE event model.
[Conceptual understanding] Event propagation can be divided into two types: one is capture propagation, and the other is bubbling propagation.
Capture propagation: that is, the event is transmitted from the outside to the inside, and the event occurs at each level
Bubble propagation; that is, the event is transmitted from the inside to the outside, and the event occurs at each level , not all events will bubble
(1) Standard event model
The standard event model can occur both bubbling propagation and capture propagation.
【Event registration function】
addEventListener() This method registers an event handler for a specific element. It has three parameters. The first parameter is the event name. Note that there is no The prefix is on, and the second one is the processing function (of course it can also be an anonymous function). When an event occurs, the system will automatically pass in an Event object to the first parameter of the function. The last parameter is a Boolean value. If it is true, it is only used in the event capture and propagation stage. If it is false, it is only used in the event bubbling propagation stage. Generally, we set it to false
For example: element. addEventListener("click",f,false) //where f is a function
f function can be defined like this: function f(e){...} //The parameters in it represent the Event when the event occurs Object
One of the major advantages of advanced event processing is that multiple event processing functions can be registered for the same element. The order in which these event functions are executed cannot be determined, but generally they are executed in the order of registration. If duplicate event functions are registered, only the first one will take effect.
removeEventListener() This method is used to remove event registration. Its three parameters are the same as addEventListener()
(2) IE event model
IE event model only supports event bubbling propagation
【Event registration function】
attactthEvent() This method has only 2 parameters, one is the event name, note the prefix on, and the second is the event processing function. For example, element.attachEvent("onclick",f)
The Event object of the IE event model is not passed as a parameter to the event processing function when the event occurs. The Event object of IE is a global object of the window. Access is only available when an event occurs.
So the f function can be defined like this: function f(){var e=window.event;......} //where e obtains the Event object
detachEvent() This method is used Cancel event registration, the parameters are the same as attachthEvent()
An important difference between addEventListener() and attachthEvent() is that the this keyword in the event processing function registered by attachthEvent() always points to the window object, while addEventListener () This in the registered event processing function points to the element where the event occurred
(3) Comparison of the Event objects of IE and the standard event model
IE event object |
IE event object |
Standard Event Object |
Standard Event Object |
altKey |
true means the ALT key was pressed, false means not |
altKey |
true means the ALT key was pressed. false means there is no |
ctrlKey |
true means the CTRL key is pressed, false means there is no |
ctrlKey |
true means the CTRL key is pressed, false means there is no |
##shiftKey | true means the SHIFT key is pressed, false means no | shiftKey | true means The SHIFT key is pressed, false means there is no |
button | mouse event. 0 means that the mouse button is not pressed, 1 means that the left button is pressed, 2 means that the right button is pressed, 4 means that the middle button is pressed, 3 means that the left and right buttons are pressed at the same time, 5 means that the left and middle buttons are pressed, and 6 means that the right and middle buttons are pressed. Middle button, 7 means pressing the left button, middle button, and right buttonbutton | 0 means left button, 1 means middle button, and 2 means right button||
clientX | When the event occurs, the X coordinate of the mouse in the browser window (excluding toolbars, scroll bars, etc.)clientX | When the event occurs, the X coordinate of the mouse in the browser window (excluding toolbars, scroll bars, etc.)||
clientY | Same as above | clientY | Same as above |
screenX | X coordinate of the mouse on the entire screen when the event occurs | screenX | X coordinate of the mouse on the entire screen when the event occurs |
screenY | Same as above | screenY | Same as above |
type | The name of the event (such as click) | type | ##The name of the event (such as click)|
The element that caused the event |
target |
The element that caused the event |
|
keyCode |
For the keypress event, it represents the unicode character of the button, and for the keydown and keyup events, it represents the numeric code of the button | charCode |
represents the Unicode character of the key |
##keyCode | represents the numeric code of the key | ||
cancelBubble | The value is true will prevent the event from continuing to bubble upward | stopPropagation | You can call this method to prevent the event from continuing to bubble upward |
cancelBubble | true means the event bubbling has been cancelled, false means not | ||
returnValue | A value of false will prevent the default behavior of the event | preventDefault() | Call this method to prevent the default behavior of the event |
Get the X coordinate of the mouse relative to the element that caused the event when the event occurs, that is, the upper left corner of the element that caused the event itself (without calculating padding and margin) is the origin | layerX | When the element that triggers the event does not have dynamic positioning, the X coordinate of the mouse relative to the closest parent element that has dynamic positioning set to the element that triggered the event is returned, based on the border of the parent element. The upper left corner is the origin. | When the element that triggers the event is set to dynamic positioning, the X coordinate of the mouse relative to the element that triggered the event is returned, with the upper left corner of the element's boundary as the origin.|
Get the X coordinate of the mouse relative to the most recent dynamically positioned parent element of the element that triggered the event, based on the parent element The upper corner of the border i is the origin |
Related articles:
How to use JSON ##js Through html() and text( ) method to get and set the display value of the p tag## Summary of the use of the three JS loading methods
The above is the detailed content of Using js to implement event processing model. For more information, please follow other related articles on the PHP Chinese website!

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

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