Home Web Front-end JS Tutorial Detailed explanation of JavaScript dom event object and IE event object instance

Detailed explanation of JavaScript dom event object and IE event object instance

Jul 24, 2017 pm 03:45 PM
ie javascript js

Event object: When an event on the DOM is triggered, an event object event will be generated. This object contains all information related to the event, including the element that caused the event, The type of event and other information related to the specific event.

Event object in DOM

A DOM-compatible browser will pass an event object into the event handler, regardless of When specifying the event handler, whether the DOM0 or DOM2 method is used, the event object will be passed in. The event object only exists during the execution of the event handler. Once the event handler completes execution, the event object will be destroyed. The following is a code example:

var btn = document.getElementById("myBtn");
btn.onclick = function(event) {
 console.log(event.type); // "click"
}
btn.addEventListener("click", function(event) {
 console.log(event.type);
}, false);
Copy after login

The event object contains properties and methods related to the specific event that created it. The types of events triggered are different, and the available property methods are also different. But all events will have the following properties or methods:

  • bubbles: Boolean value, indicating whether the event bubbles

  • cancelable: Boolean value , indicating whether the default behavior of the event can be canceled

  • currentTarget: element, the element where the event handler is currently processing the event

  • defaultPrevented: Boolean Value, indicating whether the preventDefault() method has been called

  • detail: integer, detailed information related to the event

  • eventPhase: integer, calling event The stages of the handler, 1 represents the capture stage, 2 represents the target stage, and 3 represents the bubbling stage

  • preventDefault(): function, the default behavior of canceling the event, can be called when cancelable is true The method

  • stopImmediatePropagation(): function that cancels further capturing or bubbling of the event while preventing any event handlers from being called

  • stopPropagation (): Function, cancels further capturing or bubbling of the event. This method can be called when bubbles is true

  • target: element, the target of the event

  • trusted: Boolean value, when true, it means that the event was generated by the browser, otherwise it means that the event was created through JS

  • type: String, the type of event being triggered

  • view: The abstract view associated with the event, equivalent to the window object where the event occurred

The following code example shows the usage of some of the above attributes, It can also help us further understand the event flow. Suppose there is a button "myBtn" on the page. When the button is clicked, both this and currentTarget are equal to the body element because the event handler is registered on the body element. The value of target is equal to the button element, because it is the real target of the click event. Since there is no event handler registered on the button, the "click" event bubbles up to document.body before being processed.

document.body.onclick = function(event) {
 console.log(event.currentTarget === document.body); // true
 console.log(this === document.body); // true
 console.log(event.target === document.getElementById("myBtn")); // true
};
Copy after login

Look at another example. In the following code, the stopPropagation() method cancels further capturing or bubbling of events. When I click the button, the click event handlers on the button and body elements should be triggered due to the event bubbling mechanism, outputting "From Bth..." and "From Body...". Now the click event is blocked from propagating further in the DOM hierarchy after firing on the button element, so the event handler on the body will not be fired.

var btn = document.getElementById("myBtn");
btn.onclick = function(event) {
 console.log("From Bth ...");
 event.stopPropagation(); // 停止事件传播
};
document.body.onclick = function() {
 console.log("From Body ...");
};
Copy after login

Event object in IE

In IE, when using the DOM0 method to add an event handler, the event object is used as the window object. A property exists. If it is added through the attachEvent() method, the event object is passed into the event processing function as a parameter. The following is a code example:

var btn = document.getElementById("myBtn");
btn.onclick = function() {
 var event = window.event;
 console.log(event.type); // "click"
};
btn.attachEvent("onclick", function(event) {
 console.log(event.type); // "click"
});
Copy after login

IE's event object also contains properties and methods related to the event that created it. These properties and methods will also vary depending on the event type. But all event objects will contain the following properties:

  • cancelBubble: Boolean value, readable and writable, defaults to false. Cancel event bubbling when set to true

  • returnValue: Boolean value, readable and writable, defaults to true. The default behavior of canceling the event when set to false

  • srcElment: element, the target element of the event, the same as the target attribute in the DOM

  • type: string, event type

In IE, the scope of the event handler is determined based on how it is specified. The value of this does not necessarily point to the target element of the event. . Therefore, it is safer to use the srcElement attribute. Please look at the code example below. In the first method, the value of this is the target element, and in the second method, as mentioned earlier, the event handler of this method is executed in the global scope, so the value of this is window.

var btn = document.getElementById("myBtn");
btn.onclick = function() {
 console.log(window.event.srcElement === this); // true
}
btn.attachEvent("onclick", function(event) {
 console.log(event.srcElement === this); // false
});
Copy after login

The above is the detailed content of Detailed explanation of JavaScript dom event object and IE event object instance. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) Feb 10, 2024 am 10:30 AM

More and more users are starting to upgrade the win11 system. Since each user has different usage habits, many users are still using the ie11 browser. So what should I do if the win11 system cannot use the ie browser? Does windows11 still support ie11? Let’s take a look at the solution. Solution to the problem that win11 cannot use the ie11 browser 1. First, right-click the start menu and select "Command Prompt (Administrator)" to open it. 2. After opening, directly enter "Netshwinsockreset" and press Enter to confirm. 3. After confirmation, enter "netshadvfirewallreset&rdqu

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

See all articles