Table of Contents
1 Summary of common events
1.1 Mouse events
1.2 Keyboard events
Home Web Front-end JS Tutorial The latest summary of common JavaScript DOM events!

The latest summary of common JavaScript DOM events!

Aug 07, 2022 am 11:05 AM
javascript dom

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		鼠标移动
Copy after login

1.2 Keyboard events

keydown		键盘按键按下
keyup		键盘按键抬起
keypress	键盘按键按下,用于可输入字符按键
Copy after login

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.

#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.

1.3 Document event

load				页面中所有的一切加载完毕就会触发,可以监听到window上或者body元素
DOMContentLoaded	页面中所有的元素加载完毕就会触发,可以监听在window或者document上, 只能使用 						addEventListener 监听事件
beforeunload		当关闭网页的时候触发
Copy after login

The 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.

1.4 Form event

submit		当表单提交的时候触发,该事件监听到form元素	
reset		当表单重置的时候触发,该事件监听到form元素
focus		当表单控件获取焦点的时候触发
blur		当表控件单失去焦点的时候触发
select		输入框或文本域中的内容被选中
change		对于输入框,内容改变且失去焦点才会触发;适合用于select
Copy after login

1.5 Picture event

load		图片文件下载完毕
error		图片加载失败
Copy after login

1.6 Other events

resize		监听到 window上,视口大小发生改变
scroll		监听到window或者是具有滚动体的元素,页面或元素中的内容发生滚动就触发。
Copy after login

2 Event object

2.1 Get Event object

Set 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 MouseEvent

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 KeyBorardEvent

keyCode		获取按键对应的编码值
which		同 keyCode
key			获取按键对应的字符值
Copy after login

2.4 Properties and methods common to all types of event objects

type				获取事件名
timeStamp			获取触发事件时距离打开页面时的毫秒数
target				获取目标元素
stopPropagation()	阻止事件冒泡
preventDefault()	阻止浏览器默认行为
Copy after login

2.5 Prevent event bubbling

Execute

event.stopPropagation() in the callback function of the event to prevent bubbling.

2.6 The default behavior of the browser

① What are the default behaviors of the browser

超链接点击跳转
表单的提交和重置
右键弹出系统菜单
等...
Copy after login

② Prevent the default behavior of the browser

In the callback function of the event Call

event.preventDefault() to prevent the default behavior.

Note: If you use the second method to listen to events, return false in the callback function can also prevent the default behavior.

3 Event delegation

The 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:

  • 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.

Related recommendations: [

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

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 implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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 implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

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.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

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

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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 forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

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

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

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

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

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).

See all articles