What are the common binding events in jquery?
There are four commonly used binding events in jQuery, namely: 1. "on()" method, which adds one or more event handlers to the selected element and sub-elements; 2. "bind()" ” method, binds a listening function of a specific event type to the selected element; 3. “live()” method, which was removed in version 1.9; 4. “delegate()” method, adds a or multiple event handlers and run the specified function.
Operating system for this tutorial: Windows 10 system, jQuery3.6.0 version, Dell G3 computer.
There are four commonly used binding events in jquery:
1. on() method (preferred method)
on () method adds one or more event handlers on the selected element and sub-elements.
Since jQuery version 1.7, the on() method is the new replacement for the bind(), live(), and delegate() methods. This method brings a lot of convenience to the API and is recommended because it simplifies the jQuery code base.
Parameters:
event: Required. Specifies one or more events or namespaces to be removed from the selected elements. Multiple event values separated by spaces. Must be a valid event
data: Optional. Specifies that event handlers can only be added to specified child elements (and not the selector itself, such as the deprecated delegate() method).
function: Optional. Specifies a function to run when an event occurs.
map: Specifies event mapping ({event:function, event:function, ...}), containing one or more events to be added to the element , and a function to run when the event occurs.
Syntax: $(selector).on(event,childSelector,data,function,map)
Example: Add a single element to the p element Click event
$("p").on("click",function(){ alert("The paragraph was clicked."); });
2. bind() method
bind() function is to bind a specific event type to the selected element Listening function,
Parameters:
type:Required. Event type, such as click, change, mouseover, etc.;
data:Optional. The parameters passed into the listening function are obtained through event.data. Optional;
function:Required. Specifies the function (listening function) to be run when an event occurs, and the event object can be passed in. The event here is the event object encapsulated by jQuery, which is different from the native event object. You need to pay attention when using it.
map:Specifies event mapping ({event:function, event:function, ...}), containing elements to be added to One or more events, and a function to run when the event occurs
Syntax: $(selector).bind(event,data,function,map)
Example: Add a click event to
$("p").bind("click",function(){ alert("单击p元素"); });
3. live() method
live (), was removed in version 1.9. Please use on() method instead. Adds one or more event handlers to the current or future selected elements. (Still summarized here)
Parameters:
event: required . Specifies one or more events to be added to the element. Multiple event values separated by spaces. Must be a valid event.
data:Optional. Extra parameters passed to the function
function:Required. When an event occurs, the function
is run. Syntax: $(selector).live(event,data,function)
Example: When the button is clicked, hide or show the element:
$("button").live("click",function(){ $("p").slideToggle(); });
四、delegate()方法
delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。使用 delegate() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素)。
参数:
childSelector:必需。规定要添加事件处理程序的一个或多个子元素。
event:必需。规定添加到元素的一个或多个事件。由空格分隔多个事件值。必须是有效的事件。
data:可选。传递到该函数的额外参数
function:必需。当事件发生时,运行的函数
语法:$(selector).delegate(childSelector,event,data,function)
举例:当单击 元素时,改变所有 元素的背景颜色: The above is the detailed content of What are the common binding events in jquery?. For more information, please follow other related articles on the PHP Chinese website!$("div").delegate("p","click",function(){
$("p").css("background-color","pink");
});

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











Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code: