Home  >  Article  >  Web Front-end  >  The latest summary of common JavaScript DOM events!

The latest summary of common JavaScript DOM events!

藏色散人
藏色散人forward
2022-08-07 11:05:501727browse

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.

#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		当关闭网页的时候触发

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

1.5 Picture event

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

1.6 Other events

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

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:右键

2.3 Properties and methods of the keyboard event object KeyBorardEvent

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

2.4 Properties and methods common to all types of event objects

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

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

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

② 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:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete