table of Contents
Describe the w3c event flow
What is event flow? (Event model, event mechanism).
The response sequence of multiple node objects to the same event is called the event stream.
W3C event flow:
- Execution capture stage: Netscape browser proposes that events are executed sequentially from the least precise object (document) to the most precise target element (target).
- Execute the target event.
- Execution bubbling stage: Internet Explorer proposes that events are executed sequentially from the most precise target element (target) to the least precise object (document).
- The event determines in which stage it executes according to whether it is bubbling or capturing.
Comparison of window.onload and DOMContentLoaded
-
Window's onload event: it can only be triggered
when theDOM
node has been loaded and all resources have been loaded. -
DOMContentLoaded
Event:
When the DOM node is loaded, it will execute and no longer wait for resources.
TheDOM2
function binding must be processed in advance for the event of the level.
Talk about DOM0 and DOM2 binding events separately
DOM0
Dynamic binding: The anonymous function is directly assigned to the event attribute of the DOM object, and the anonymous function will be executed when the event is triggered.
Features:
- The same event can only be bound to the same element once, otherwise it will be overwritten.
- Only bubbling can be performed.
Cancel event:
Simply assign the value of the event attribute to null.
DOM2
Event handler: w3c provides an event handleraddEventListener()
designed to handle the event.
Features:
- You can bind the same event multiple times to the same element.
- Can control bubbling and capture.
Delete event:
How to use removeEventListener()
it.
scenes to be used:
- Binding the same event to the same element multiple times does not want to be overwritten.
- Can control capture or bubbling.
- DOM2 level events can only be bound through event handling functions (
DOMContentLoaded
).
|| and the return value of the && operator?
- || and && will first perform conditional judgment on the first operand, if it is not a Boolean value, perform Boolean coercion first, and then perform conditional judgment.
- For ||, if the conditional judgment result is true, the value of the first operand is returned, and if it is false, the value of the second operand is returned.
- && On the contrary, if the condition is true, it returns the value of the second operand, and if it is false, it returns the value of the first operand.
- || and && return the value of one of their operands, not the result of conditional judgment.
Write a general event listener function
function (element, type, handler, method) {
//DOM2级事件处理程序,false表示在冒泡阶段处理事件程序
if (element.addEventListener) {
element.addEventListener(type, handler, method || false);
}
//IE事件处理程序
else if (element.attachEvent) {
element.attachEvent("on" + type, handler);
}
//DOM0级
else {
element["on" + type] = handler;
}
}