The latest summary of common JavaScript DOM events!
This article summarizes the common events of JS DOM, which has certain reference value. Interested friends can refer to it.
1 Summary of common events
1.1 Mouse events
click 单击 dblclick 双击 contextmenu 右击 mouseover 鼠标悬停在元素上, 建议用 mouseenter 代替 mouseout 鼠标离开元素,建议用 mouseleave 代替 mouseenter 鼠标悬停在元素上 mouseleave 鼠标离开元素 mousedown 鼠标按键按下 mouseup 鼠标按键抬起 mousemove 鼠标移动
1.2 Keyboard events
keydown 键盘按键按下 keyup 键盘按键抬起 keypress 键盘按键按下,用于可输入字符按键
1. Which elements can listen to keyboard events?
① document
② Elements that can get focus (form controls, especially input elements)
2 . What is the difference between keydown and keypress?
① keydown can be triggered by pressing all keys, and it cannot distinguish between uppercase and lowercase keys.
② keypress can only be triggered when the key that can input characters is pressed, and the keys can be case-sensitive.
1.3 Document event#3. How to get which key is pressed?
Use the attributes in the event object:
evnet.keyCode to get the ascii value corresponding to the key
- ##event .which is the same as keyCode
- #event.key to get the character value of the key.
load 页面中所有的一切加载完毕就会触发,可以监听到window上或者body元素
DOMContentLoaded 页面中所有的元素加载完毕就会触发,可以监听在window或者document上, 只能使用 addEventListener 监听事件
beforeunload 当关闭网页的时候触发
Copy after login
load 页面中所有的一切加载完毕就会触发,可以监听到window上或者body元素 DOMContentLoaded 页面中所有的元素加载完毕就会触发,可以监听在window或者document上, 只能使用 addEventListener 监听事件 beforeunload 当关闭网页的时候触发
1.4 Form eventThe difference between load event and DOMContentLoaded event:
① load The event is triggered when everything on the page is loaded, including elements and external resources. ② The DOMContentLoaded event can be triggered when all elements in the page are loaded, excluding external resources.
submit 当表单提交的时候触发,该事件监听到form元素
reset 当表单重置的时候触发,该事件监听到form元素
focus 当表单控件获取焦点的时候触发
blur 当表控件单失去焦点的时候触发
select 输入框或文本域中的内容被选中
change 对于输入框,内容改变且失去焦点才会触发;适合用于select
Copy after login
1.5 Picture eventsubmit 当表单提交的时候触发,该事件监听到form元素 reset 当表单重置的时候触发,该事件监听到form元素 focus 当表单控件获取焦点的时候触发 blur 当表控件单失去焦点的时候触发 select 输入框或文本域中的内容被选中 change 对于输入框,内容改变且失去焦点才会触发;适合用于select
load 图片文件下载完毕
error 图片加载失败
Copy after login
1.6 Other eventsload 图片文件下载完毕 error 图片加载失败
resize 监听到 window上,视口大小发生改变
scroll 监听到window或者是具有滚动体的元素,页面或元素中的内容发生滚动就触发。
Copy after login
2 Event object2.1 Get Event objectSet the first formal parameter to the event callback function to obtain the event object. Different types of events obtain different Event object types. 2.2 Properties and methods of the mouse event object MouseEventresize 监听到 window上,视口大小发生改变 scroll 监听到window或者是具有滚动体的元素,页面或元素中的内容发生滚动就触发。
offsetX / offsetY 获取鼠标在目标元素上的坐标位置
clientX / clientY 获取鼠标在视口上的坐标位置
pageX / pageY 获取鼠标在页面上的坐标位置
screenX / screenY 获取鼠标在屏幕上的坐标位置
button 获取按的是哪个鼠标按键, 0:左键; 1:中间键; 2:右键
Copy after login
2.3 Properties and methods of the keyboard event object KeyBorardEventoffsetX / offsetY 获取鼠标在目标元素上的坐标位置 clientX / clientY 获取鼠标在视口上的坐标位置 pageX / pageY 获取鼠标在页面上的坐标位置 screenX / screenY 获取鼠标在屏幕上的坐标位置 button 获取按的是哪个鼠标按键, 0:左键; 1:中间键; 2:右键
keyCode 获取按键对应的编码值
which 同 keyCode
key 获取按键对应的字符值
Copy after login
2.4 Properties and methods common to all types of event objects keyCode 获取按键对应的编码值 which 同 keyCode key 获取按键对应的字符值
type 获取事件名
timeStamp 获取触发事件时距离打开页面时的毫秒数
target 获取目标元素
stopPropagation() 阻止事件冒泡
preventDefault() 阻止浏览器默认行为
Copy after login
2.5 Prevent event bubblingExecute type 获取事件名 timeStamp 获取触发事件时距离打开页面时的毫秒数 target 获取目标元素 stopPropagation() 阻止事件冒泡 preventDefault() 阻止浏览器默认行为
event.stopPropagation() in the callback function of the event to prevent bubbling.
超链接点击跳转
表单的提交和重置
右键弹出系统菜单
等...
Copy after login
② Prevent the default behavior of the browserIn the callback function of the event Call 超链接点击跳转 表单的提交和重置 右键弹出系统菜单 等...
event.preventDefault() to prevent the default behavior.
3 Event delegationThe event is monitored on the ancestor element, and the target element is judged. If the target element meets the conditions, relevant operations are performed. Advantages of event delegation:Note: If you use the second method to listen to events, return false
in the callback function can also prevent the default behavior.
- For monitoring the same event on a large number of elements, using event delegation is more efficient than traversing and listening one by one.
- Using event delegation allows newly added elements to respond to events.
JavaScript video tutorial]
The above is the detailed content of The latest summary of common JavaScript DOM events!. For more information, please follow other related articles on the PHP Chinese website!

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

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
