A detailed explanation of event objects, event source objects and event streams in js

藏色散人
Release: 2022-08-07 09:51:13
forward
2212 people have browsed it

Event object, event source object, event flow analysis and understanding in js

Event object (event)

  • What are events: Events refer to all events that can occur in js and are monitored, such as: (mouse, keyboard and browser window changes, etc.)

  • What are events Object (event): In layman's terms, it is an object that records various information about the event.
    What needs to be noted here is that the event object will have compatibility issues. In browsers other than IE, it is event, but in non-browsers it is window.event,

btn.onclick = function(event){let e = event || window.event}
Copy after login

Event source object

Simply put, it means that the event specifically occurs on that object. For the element element, the event The source object refers to the element you clicked. There are also browser compatibility issues:

  • In fireFox it is event.srcElement
  • In IE it is event.target
    For compatible writing methods, refer to the event object

Event flow

Event flow is mainly divided into two categories: 1. Capture event 2. Bubbling event triggering order is to capture first and then bubble
But if it is subdivided, there will be a target stage when the bubbling stage is captured, that is, the operation stage for the specific DOM element to be operated

Capture events

The uppermost node first receives the event, and then propagates it downward to the specific node. eg: When the user clicks on the p element and uses event capture, the click event will be propagated in the order of document>htm>body>p. The mode of transmission is from outside to inside.

Bubble event

Contrary to capture event, it is passed from inside to outside. When the user clicks p, it will be passed to the parent, p>body>html . ***Because this feature is often used for event delegation.

Event Delegate

We bind the same events to be triggered by all child elements to the parent element, which can reduce DOM operations and improve performance. The specific usage method is to use the event source object method.

  • 1
  • 2
  • 3
  • 4
  • 5
Copy after login

To bind click events to li, usually our approach is to loop through the click events to li

let oLi = document.querySelectorAll("li") for(let i; i < oLi.length; i++){ oLi[i].onclick = function(){ console.log("i") } }
Copy after login

and the method of using event delegation is

let oUl = document.querySelector("ul") oUl.onclick = function(event){ let e = event || window.event console.log(e.target.innerHTML) }
Copy after login
  • Advantages
  • Improve performance, no need to loop through all elements to bind events one by one.
  • Flexible, new elements created dynamically do not require rebinding events.

Prevent event bubbling and prevent default events

Operation to prevent event bubbling (compatibility writing)

***Some events do not require bubbling operations

function stopBubble(event){ var e = event||window.event //事件对象兼容写法 e.stopProgation() ? e.stopProgation() : e.cancelBubble = true //IE兼容写法}
Copy after login

Block default events (compatible writing)

***Block a Tags and right mouse button default jump and menu events

function cancelHandle(event){ var e = event||window.event e.preventDefault() ? e.preventDefault() : e.returnValue = false/*ie*/}
Copy after login

Related recommendations: [JavaScript Video Tutorial]

The above is the detailed content of A detailed explanation of event objects, event source objects and event streams in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!