Click () Bind () Live () delegate () Difference

Source: Internet
Author: User

Click (), Bind (), Live () are all methods used when executing an event () Click event Method:

$ ("a"). Click (function () {   alert ("Hello");});

2. In bind (), jquery all JavaScript event objects, such as focus, mouseover, and resize, can be passed in as type parameters.

var message = "Left"; $ ("a"). Bind ("click", Function () {   alert (message);   return false;});

var message = "Left"; $ ("a"). Bind ("click", {msg:message}, function (e) {   alert (;   return false;}); var message = "Right"; $ ("a"). Bind ("ContextMenu", {msg:message}, function (e) {   alert (;   return false;});

The bind () method is used to attach a handler to each matching element's event and return the jquery object.

. Bind (eventtype[, Evnetdata], Handler (EventObject))

Where the parameter eventtype is a string that contains one or more JavaScript event types, such as the name of a click,submit or custom event, with spaces separating each type when specifying multiple event types; EventData is the map type. Gives the data to be passed to the event handler, handler specifies the function to execute when the event is triggered, and EventObject represents the event object.

The. Bind () method attaches the event handler handler to the EventType event of each element in the matching element collection, and can also pass data to the event handler if needed. () Appends an event handler function to all matching elements, even if the element is added later.

If the element is added after the call to bind (), the corresponding event cannot be executed. The use of the Live () method allows the element to be added at the back to perform the corresponding event, as follows:

$ (""). Live ("Click", Function () {   alert ("Success");});

This way, when we click the a tag of the class live, if this a tag is added behind, it can also output "success" as usual.

One disadvantage of the live () method is that it does not support chained notation:

$ ("#test"). Children ("a"). Live ("MouseOver", function () {    alert ("Hello");}); The above notation does not output

Using delegate () can be written as:

$ ("#test"). Delegate ("A", "mouseover", function () {    alert ("Hello");});

$ (' body '). Delegate (' A:not ([target= ' _blank "]) ', ' click ', Function (e) {         if ($ (this). attr (' href ') = = Location.hash) {                    $ (window). Trigger (' Hashchange ');          } });

Stop event Propagation

$ (' a '). bind (' click ', Function (e) {      e.preventdefault ()      

if (e.stoppropagation) {     e.stoppropagation ();}

E.cancelbubble = true;   

E is a bunch of arguments that are passed in after the event is triggered, indicating that the event object

Click () Bind () Live () delegate () Difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.