


Encapsulation of adding and deleting native js events_javascript skills
To add or delete events in IE browser, use attachEvent and detachEvent. In other standard browsers, addEventListener and removeEventListener are used. The following encapsulates the addition and deletion of events. Let’s just look at the code!
/** * @description 事件绑定,兼容各浏览器 * @param target * 事件触发对象 * @param type * 事件 * @param func * 事件处理函数 */ function bind(target, type, func) { if (target.addEventListener) {// 非ie 和ie9 target.addEventListener(type, func, false); } else if (target.attachEvent) { // ie6到ie8 target.attachEvent("on" + type, func); } else { target["on" + type] = func; // ie5 } } /** * @description 事件移除,兼容各浏览器 * @param target * 事件触发对象 * @param type * 事件 * @param func * 事件处理函数 */ function unbind(target, type, func) { if (target.removeEventListener) { target.removeEventListener(type, func, false); } else if (target.detachEvent) { target.detachEvent("on" + type, func); } else { target["on" + type] = null; } }
Other additions about the meaning of the third parameter of addEventListener
The third parameter of addEventListener
The function used to add trigger events in W3C DOM is called AddEventListener, but I never know what the third parameter of this function is used for. I always set it casually and never found it. No matter how different the difference is, I finally saw the explanation when I read ppk on javascript two days ago. As for the DOM standard document that existed a long time ago, I actually never looked for information about this parameter at all.
This parameter is called useCapture, which is a boolean value, which is true or false. If true is sent, the browser will use the Capture method. If false, it will be Bubbling. It will only have an impact under certain circumstances. It is usually recommended to use false. The situation that will have an impact is that the target element (target element) has an ancestor element (ancestor element), and it also has the same event corresponding function. I think it will be clearer by looking at the picture.
Example of two-layer div block
As shown in this picture, my example has two layers of div elements, and both are set with click events. Generally speaking, if I click on the inner blue element, not only the blue element will be triggered. The click event will also trigger the click event of the red element at the same time, and the useCapture parameter controls the order of the two click events at this time. If it is false, then bubbling will be used. It is an inside-out process, so the click event of the blue element will be executed first and then the click event of the red element. If it is true, it will be capture. In contrast to bubbling, it will be executed by Outside-in, the click event of the red element will be executed first and then the click event of the blue element will be executed. Attached are two examples, capture and bubbling. The only difference between the two files is this parameter. It can be found that the order of events is different.
What if elements in different layers use different useCaptures? That is, it will first search for the event set to capture from the outermost element to the target element. After reaching the target element and executing the event of the target element, it will then look for the event set to bubbling outward along the original path.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics


![Event ID 4660: Object deleted [Fix]](https://img.php.cn/upload/article/000/887/227/168834320512143.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Some of our readers encountered event ID4660. They're often not sure what to do, so we explain it in this guide. Event ID 4660 is usually logged when an object is deleted, so we will also explore some practical ways to fix it on your computer. What is event ID4660? Event ID 4660 is related to objects in Active Directory and will be triggered by any of the following factors: Object Deletion – A security event with Event ID 4660 is logged whenever an object is deleted from Active Directory. Manual changes – Event ID 4660 may be generated when a user or administrator manually changes the permissions of an object. This can happen when changing permission settings, modifying access levels, or adding or removing people or groups

On iPhones running iOS 16 or later, you can display upcoming calendar events directly on the lock screen. Read on to find out how it's done. Thanks to watch face complications, many Apple Watch users are used to being able to glance at their wrist to see the next upcoming calendar event. With the advent of iOS16 and lock screen widgets, you can view the same calendar event information directly on your iPhone without even unlocking the device. The Calendar Lock Screen widget comes in two flavors, allowing you to track the time of the next upcoming event, or use a larger widget that displays event names and their times. To start adding widgets, unlock your iPhone using Face ID or Touch ID, press and hold

When a value is added to the input box, the oninput event occurs. You can try running the following code to understand how to implement oninput events in JavaScript - Example<!DOCTYPEhtml><html> <body> <p>Writebelow:</p> <inputtype="text"

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Commonly used events in jquery are: 1. Window events; 2. Mouse events, which are events generated when the user moves or clicks the mouse on the document, including mouse clicks, move-in events, move-out events, etc.; 3. Keyboard events, Events are generated every time the user presses or releases a key on the keyboard, including key press events, key release events, etc.; 4. Form events, such as the focus() event will be triggered when an element gains focus, and the focus() event will be triggered when it loses focus. The blur() event is triggered, and the submit() event is triggered when the form is submitted.

How to implement calendar functions and event reminders in PHP projects? Calendar functionality and event reminders are one of the common requirements when developing web applications. Whether it is personal schedule management, team collaboration, or online event scheduling, the calendar function can provide convenient time management and transaction arrangement. Implementing calendar functions and event reminders in PHP projects can be completed through the following steps. Database design First, you need to design a database table to store information about calendar events. A simple design could contain the following fields: id: unique to the event

To implement the append() method in native js, specific code examples are required. When writing JavaScript code, it is often necessary to add new content to specified elements in the web page. A common operation is to set the HTML content of the element through the innerHTML attribute. However, using the innerHTML attribute sometimes results in the loss of event listeners, styles, etc. inside the element. In order to better implement the function of adding content, we can implement an append() method ourselves. The append() method can

Methods for building event-based applications in PHP include using the EventSourceAPI to create an event source and using the EventSource object to listen for events on the client side. Send events using Server Sent Events (SSE) and listen for events on the client side using an XMLHttpRequest object. A practical example is to use EventSource to update inventory counts in real time in an e-commerce website. This is achieved on the server side by randomly changing the inventory and sending updates, and the client listens for inventory updates through EventSource and displays them in real time.
