JavaScript event binding learning points_basic knowledge
Event binding is divided into two types: one is traditional event binding (inline model, script model), and the other is modern event binding (DOM2-level model). Modern event binding provides more powerful and convenient functions over traditional binding.
1 Problems with traditional event binding
The inline model in traditional event binding will not be discussed and is rarely used. Let's take a look at the script model first. The script model assigns a function to an event handling function. Traditional binding such as:
1 2 3 4 5 6 |
|
Problem 1: An event handling function triggers two events
If a page has two or more js, and the first js is developed by the first programmer, the second js is developed by the second programmer. The first window.onload is overwritten, such as
1 2 3 4 5 6 7 |
|
The result just printed Mr.lee
In fact, there are ways to solve this problem. Take a look at the following two forms.
a:
1 2 3 4 5 6 7 8 9 10 11 |
|
b:
1 2 3 4 5 6 7 8 9 10 11 |
|
So there is a solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Question 2: Event Switcher
Switch a div with the ID of box, let the background red and blue inside switch directly, and pop up the box once before switching, such as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Although the above code implements the switching function, the pop-up box is only executed once.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
According to the above code, an error occurred in the comment. The solution is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
Two W3C event handling functions
addEventListener() and removeEventListener()
There are two W3C event handling functions, addEventListener() and removeEventListener().
//W3C comes with two add events and deletion events
1. Coverage problem, solved
1 2 3 4 5 6 7 8 9 10 11 |
|
2. The problem of blocking the same function is solved
1 2 3 4 5 6 |
|
3. Is it possible to pass this and solve it
Example 1:
1 2 3 4 5 6 |
|
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
4. Add an additional method. Will it be overwritten or can only be executed once? Solve
1 2 3 4 5 6 7 |
|
To sum up: W3C has solved these problems perfectly and is very easy to use. However, IE8 and previous browsers do not support it. Instead, they use their own events. Of course, IE9 has fully supported this of W3C. Two event handling functions.
W3C can set bubbling and capturing methods.
Browsers that support the W3C standard use the addEventListener(event,fn,useCapture) method when adding events. The third parameter useCapture in the base is a Boolean value, which is used to set whether the event is executed during event capture or when the event occurs. Executed when soaking. Browsers that are not compatible with W3C (IE) use the attachEvent() method. This method has no relevant settings. However, IE's event model is executed by default when the event bubbles up, that is, when useCapture is equal to false, so put it in It is safer to set useCapture to false when handling events, and it also achieves browser compatibility.
Event capture phase: The event starts from the top level label and searches downward until the event target (target) is captured.
Event bubbling stage: The event starts from the event target (target) and bubbles up to the top-level label of the page.
The spread of events can be stopped:
In W3c, use the stopPropagation() method
Set cancelBubble = true under IE;
3. IE event handling function
attachEvent() and detachEvent()
IE implements two methods similar to those in DOM: attachEvent() and detachEvent(). Both methods accept the same parameters: event name and function.
When using these two sets of functions, let’s first talk about the differences: 1. IE does not support capturing, only bubbling; 2. IE adding events cannot block duplicate functions; 3. this in IE points to window instead of a DOM object. 4. In traditional events, IE cannot accept event objects, but using attchEvent can, but there are some differences.
1. The coverage problem is solved, but there are differences. The result is Mrs.Lee, Mr.Lee, and finally Lee
1 2 3 4 5 6 7 8 9 10 |
|
2. The problem of blocking the same function has not been solved.
1 2 3 4 5 6 |
|
3. Can this be passed? No, this refers to window. Need to use call method.
1 2 3 4 5 6 7 |
|
The next way is to pass window.event.srcElement. The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
4. Add an additional method. Will it be overwritten or can only be executed once? Solve it.
In traditional binding, IE cannot accept event objects through parameter passing like W3C, but it can be done using attachEvent().
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Cross-browser compatibility
Cross-browser events
1 2 3 4 5 6 7 |
|
Cross browser removal event
1 2 3 4 5 6 7 |
|
Get target object across browsers
1 2 3 4 5 6 7 |
|
调用方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
四.事件对象的其他补充
relatedTarget事件
w3c中的一个relatedTarget事件。
例如:
1 2 3 4 5 6 7 8 9 10 |
|
IE提供了两组分别用于移入移出的属性fromElement和toElement,分别对应mouseover和mouseout。
1 2 3 4 5 6 7 8 9 10 |
|
PS:fromElement和toElement如果分别对应相反的鼠标事件,没有任何意义。
剩下要做的就是跨浏览器兼容操作:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
屏蔽跳转操作
取消事件的默认行为有一种不规范的做法,就是返回false。
1 2 3 4 |
|
PS:虽然return false;可以实现这个功能,但是有漏洞。
第一:必须写到最后,这样导致中奖的代码执行后,有可能执行不到return false;
第二:return false 写到最前那么之后的自定义操作就失效了。
所以最好的办法应该是在最前面就阻止默认行为,并且后面的代码还可以执行。
1 2 3 4 5 6 7 8 9 |
|
那么跨浏览器的兼容:
1 2 3 4 5 6 7 8 |
|
右键菜单contextmenu
兼容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
PS:contextmenu事件很常用,这直接导致浏览器兼容性较为稳定。
卸载前事件:beforeunload
这个事件可以帮助在离开本页的时候给出相应的提示,“离开”或者“返回”操作。
1 2 3 |
|
鼠标滚轮(mousewheel)和DOMMouseScroll
用于获取鼠标上下滚轮的距离
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
PS:通过浏览器检测可以确定火狐只执行DOMMouseScroll。
DOMContentLoaded事件和readystatechange事件
DOMContentLoaded事件和readystatechange事件,有关DOM加载方面的事件。

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



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.
