The interaction between JavaScript and HTML is achieved through events. JavaScript uses an asynchronous event-driven programming model. When something specific happens to the document, browser, element, or object related to it, the browser will generate an event. If JavaScript cares about a specific type of event, it can register a handler to be called when that type of event occurs.
Event flow
The event flow describes the order in which events are received from the page. For example, if there are two nested divs and the inner div is clicked, will the inner div trigger the click event first or the outer div? There are currently three main models
IE's event bubbling: the event is initially received by the most specific element, and then propagates upwards to less specific elements
Netscape's event capture: less specific nodes receive events earlier, while the most specific elements receive events last, contrary to event bubbling
DOM event flow: DOM level 2 events stipulate that the event flow includes three stages, the event capture stage, which is in the target stage, and the event bubbling stage. The first thing that happens is the event capture, which provides an opportunity to intercept the event, and then the actual target receives the event. , and finally the bubble sentence stage.
Opera, Firefox, Chrome, and Safari all support DOM event streaming. IE does not support event streaming and only supports event bubbling
If there is the following html, click on the div area
Event handler
We also call it an event listener. An event is a certain action performed by the user or the browser itself. For example, click, load, moseover, etc. are all event types (commonly known as event names), and the method to respond to an event is called an event handler or event listener or event handler. The event handler name is: on event type.
Understanding this, let’s see how to add event handlers to elementsEach event supported by the HTML event handler element can be specified using an HTML attribute with the same name as the corresponding event handler. The value of this attribute should be executable JavaScript code. We can add a click event handler for a button
Copy code
IE does not support the addEventListener and removeEventListener methods. Instead, it implements two similar methods, attachEvent and detachEvent. Both methods receive the same two parameters, event handler name and event handler method. Since IE refers to Supports event bubbling, so added programs will be added to the bubbling stage.
Using attachEvent to add an event handler can be as follows
Event handlers added using attachEvent can be removed through detachEvent. The conditions are also the same parameters. Anonymous functions cannot be removed.
We can see from the previous content that the methods of adding and removing event handlers are different in different browsers. If we want to write a cross-browser event handler, we must first understand the processing in different browsers. Differences in event handlers
There are several main differences between addEventListener and attachEvent when adding event handlers
1. The number of parameters is different. This is the most intuitive. addEventListener has three parameters, attachEvent has only two, and the event handler added by attachEvent can only Occurs in the bubbling phase. The third parameter of addEventListener can determine whether the added event handler is processed in the capturing phase or the bubbling phase (we generally set it to the bubbling phase for browser compatibility)
2. The first parameter has different meanings. The first parameter of addEventListener is the event type (such as click, load), while the first parameter of attachEvent specifies the event processing function name (onclick, onload)
3. The scope of event handlers is different. The scope of addEventListener is the element itself, this refers to the triggering element, and the attachEvent event handler will run in the global variable. This is window, so the example just now will Return undefined, not element id
4. When adding multiple event handlers for an event, the execution order is different. addEventListener will be added in the order of addition, while attachEvent will add multiple event handlers in an irregular order (when there are few methods added, most of them will be executed in the order added). The order of addition is executed in reverse order, but if you add too much, it will become irregular), so when adding more than one, it is better not to rely on the order of execution. If it depends on the order of function execution, it is best to handle it yourself, and don't count on the browser.
After understanding these four differences, we can try to write a method of adding event handlers with better browser compatibility
Copy code
The code is as follows:var btnClick = document.getElementById('btnClick');
event对象包含与创建它的特定事件有关的属性和方法,触发事件的类型不同,可用的属性和方法也不同,但是所有事件都会包含
属性/方法 |
类型 |
读/写 |
说明 |
bubbles | Boolean | 只读 | 事件是否冒泡 |
cancelable | Boolean | 只读 | 是否可以取消事件的默认行为 |
currentTarget | Element | 只读 | 事件处理程序当前处理元素 |
detail | Integer | 只读 | 与事件相关细节信息 |
eventPhase | Integer | 只读 | 事件处理程序阶段:1 捕获阶段,2 处于目标阶段,3 冒泡阶段 |
preventDefault() | Function | 只读 | 取消事件默认行为 |
stopPropagation() | Function | 只读 | 取消事件进一步捕获或冒泡 |
target | Element | 只读 | 事件的目标元素 |
type | String | 只读 | 被触发的事件类型 |
view | AbstractView | 只读 | 与事件关联的抽象视图,等同于发生事件的window对象 |
要阻止事件的默认行为,可以使用preventDefault()方法,前提是cancelable值为true,比如我们可以阻止链接导航这一默认行为
IE中的事件对象访问IE中的event对象有几种不同的方式,取决于指定事件处理程序的方法。直接为DOM元素添加事件处理程序时,event对象作为window对象的一个属性存在
属性/方法 |
类型 |
读/写 |
说明 |
cancelBulle | Boolean | 读/写 | 默认为false,设置为true后可以取消事件冒泡 |
returnValue | Boolean | 读/写 | 默认为true,设为false可以取消事件默认行为 |
srcElement | Element | 只读 | 事件的目标元素 |
type | String | 只读 | 被触发的事件类型 |
function getTarget(e) {
return e.target || e.scrElement;
}
function preventDefault(e) {
if (e.preventDefault)
e.preventDefault();
else
e.return Value = false;
}
function stopPropagation(e) {
if (e.stopPropagation)
e.stopPropagation();
else
e.can celBubble = true;
}
There are some HTML events that we often use. These events are not necessarily related to user operations. They are just briefly mentioned here. For detailed usage, you have to search Baidu and Google
1.load: Triggered on the window when the page is completely loaded, triggered on the img element when the image is loaded, or triggered on the object element when the embedded content is loaded
2.unload: The page is completely loaded Triggered on the window after uninstalling, or triggered on the object element after the embedded content is uninstalled
3.select: Triggered when the user selects a character in the text box
4.change: Triggered when the value of the text box changes after the focus changes
5.submit: Triggered when the user submits the form
6.resize: Triggered on the window when the window or frame size changes
7.scool: When the user scrolls an element with a scroll bar, on the element Trigger
8.focus: Triggered on the window and corresponding elements when the page or element gains focus
9.blur: Triggered on the window and corresponding elements when the page or element loses focus
10.beforeunload: Unload the page Previously triggered on window
11.mousewheel: Not counting HTML, triggered when the user interacts with the page through the mouse wheel and scrolls the page in the vertical direction