Home > Article > Web Front-end > 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.
click 单击 dblclick 双击 contextmenu 右击 mouseover 鼠标悬停在元素上, 建议用 mouseenter 代替 mouseout 鼠标离开元素,建议用 mouseleave 代替 mouseenter 鼠标悬停在元素上 mouseleave 鼠标离开元素 mousedown 鼠标按键按下 mouseup 鼠标按键抬起 mousemove 鼠标移动
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 当关闭网页的时候触发
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
load 图片文件下载完毕 error 图片加载失败
resize 监听到 window上,视口大小发生改变 scroll 监听到window或者是具有滚动体的元素,页面或元素中的内容发生滚动就触发。
offsetX / offsetY 获取鼠标在目标元素上的坐标位置 clientX / clientY 获取鼠标在视口上的坐标位置 pageX / pageY 获取鼠标在页面上的坐标位置 screenX / screenY 获取鼠标在屏幕上的坐标位置 button 获取按的是哪个鼠标按键, 0:左键; 1:中间键; 2:右键
keyCode 获取按键对应的编码值 which 同 keyCode key 获取按键对应的字符值
type 获取事件名 timeStamp 获取触发事件时距离打开页面时的毫秒数 target 获取目标元素 stopPropagation() 阻止事件冒泡 preventDefault() 阻止浏览器默认行为
event.stopPropagation() in the callback function of the event to prevent bubbling.
超链接点击跳转 表单的提交和重置 右键弹出系统菜单 等...
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.
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!