Jquery event processing bind, multiple events

Source: Internet
Author: User
[Jquery] jquery event processing BIND (type, [data], FN)
Data, bind, jquery, Type
BIND (type,[Data], FN)


Returned value: jquery


Overview

Bind an event handler for a specific event of each matching element.


The. BIND () method is the main method used to add behavior to the document. All JavaScript event objects, such


Focus, Mouseover, and resize can both be passed in as the type parameter.


Jquery also provides some simple ways to bind these standard event types, such as. Click () to simplify. Bind


('Click '). Blur, focus, focusin, focusout, load, resize, scroll,


Unload, click, dblclick, mousedown, mouseup, mousemove, Mouseover,


Mouseout, mouseenter, mouseleave, change, select, submit, keydown,


Keypress, keyup, error.


Any string used as the type parameter is legal; if a string is not a native JavaScript event


The event handler is bound to a custom event. These custom events will never be created


The browser is triggered, but can be manually triggered by using. Trigger () or. triggerhandler () in other code.


If the string of the type parameter contains a dot (.) character, this event is considered to have a namespace.


This dot character is used to separate the event and its namespace. For example, if you run. BIND ('click. name ',


Handler), the click in the string is the event type, and the string name is The namespace. Name


Space allows us to unbind or trigger certain types of events without triggering other events. Reference


Unbind () to obtain more information.


When an event is uploaded to an element, all the processing functions bound to the event above will be triggered. For example


If multiple event handler functions are registered, they are always triggered in the order of binding. When all bound event processing letters


After the execution is complete, the event continues to rise along the normal event bubble channel.


The basic usage of. BIND () is:

  1. $ ('# Foo'). BIND ('click', function (){
  2. Alert ('user clicked on "foo ."');
  3. });


Copy code


This Code enables the element with the ID of Foo to respond to the click event. After you click the element, a warning is displayed.


Dialog box.


Multiple events


Multiple event types can be bound in sequence. Each event type is separated by spaces:

  1. $ ('# Foo'). BIND ('mouseenter mouseleave', function (){
  2. $ (This). toggleclass ('entered ');
  3. });


Copy code


This Code allows a <Div id = "foo"> element (the class is not set to entered initially ).


When moving in, add entered to the class, and when the mouse moves out of this Div, remove this


Class value.


In jquery 1.4, we can also bind multiple event handlers at a time by passing in a ing pair:

  1. $ ('# Foo'). BIND ({
  2. Click: function (){
  3. // Do something on click
  4. },
  5. Mouseenter: function (){
  6. // Do something on mouseenter
  7. }
  8. });


Copy code


Event processing functions


The FN parameter accepts a callback function, as shown previously. Inside the event handler function, this indicates


The Dom element bound to this function. If you want to convert this element into a jquery object


This object can be passed into $ () and re-encapsulated. For example:

  1. $ ('# Foo'). BIND ('click', function (){
  2. Alert ($ (this). Text ());
  3. });


Copy code


After the code is executed, when the user clicks the element with the ID of Foo, its text content will appear in


Warning box.


Event object


The callback function of FN can also accept a parameter. When this function is called, a javascript task


The object is passed in as a parameter.


This event object is usually unnecessary and can be omitted, because when the event processing function is bound


You will be able to clearly understand what he should do during the trigger, and usually you will have enough information. However


In some cases, you need to obtain more information about the user environment during event initialization. Complete


Event object content (English ).


Returning false in the event processing function is equivalent to. preventdefault () on the execution event object ()


And. stoppropagation ().


You can use the event object in the event handler function as follows:

  1. $ (Document). Ready (function (){
  2. $ ('# Foo'). BIND ('click', function (event ){
  3. Alert ('the mouse cursor is ('
  4. + Event. pagex + ',' + event. Pagey + ')');
  5. });
  6. });


Copy code


Note: this parameter is added to an anonymous function. This Code allows you to click the element with the ID of foo


When the report mouse clicks, it is relative to the page coordinate.


Transfer event data


Optional, the second parameter data is rarely used. If this parameter is provided, we can add


The information is passed to the event processing function. This parameter is useful in processing the problems caused by closures. Ratio


For example, we have two Event Handlers pointing to the same internal variable:

  1. VaR message = 'spoon! ';
  2. $ ('# Foo'). BIND ('click', function (){
  3. Alert (Message );
  4. });
  5. Message = 'not in the face! ';
  6. $ ('# Bar'). BIND ('click', function (){
  7. Alert (Message );
  8. });


Copy code


Because both functions have messages in their respective environments, the messages displayed during triggering are not


In the face! . The variable value is changed. To avoid this problem, we can use the message variable


Data parameters are passed in:

  1. VaR message = 'spoon! ';
  2. $ ('# Foo'). BIND ('click', {MSG: Message}, function (event ){
  3. Alert (event. Data. msg );
  4. });
  5. Message = 'not in the face! ';
  6. $ ('# Bar'). BIND ('click', {MSG: Message}, function (event ){
  7. Alert (event. Data. msg );
  8. });


Copy code


In this case, the function no longer directly points to this variable. Instead, it is passed to the data parameter by value.


Maintain the value when binding events. The first function will now display Spoon !, The second one is not in


Face!


Note: If this object is passed to the function by reference, it will make the situation extremely complex.


In addition, you can refer to the. Trigger () method to learn how to transmit data when an event is triggered, rather than when the event is bound.


Data transmission.


In jquery 1.4, data and events cannot be appended to an object, embed, or applet.


Above. This is because a severe error warning is triggered when data is appended to a Java Applet element.


Parameters

Type: String


A string containing one or more event types, such as "click" or "Submit", or a Custom Event name.


Data(Optional) bject


Extra data object passed to the event object as the event. Data Attribute Value


FN: Function


The processing function bound to the event of each Matching Element


Favorites
Share
Rating


Reply
Reference


Subscription
Top

Shanghai Div + CSS on Saturday Home-based Training hot tricks ....
  • Send Short Message
  • Add as a friend


Zhang Jiang Quan
Offline

UID
17705
Post
3
Excellent
0
Points
0
Read Permission
10
Online time
1 hour
Registration Time
2010-12-1
Last login
2010-12-3


 

Newbie


2#


Zhang Jiang QuanPosted on
| View the author only
Example

Description: The text of each section is displayed when it is clicked.

  1. $ ("P"). BIND ("click", function (){
  2. Alert ($ (this). Text ());
  3. });


Copy code


Description: You can transmit additional data before processing the event.

  1. Function handler (event ){
  2. Alert (event. Data. Foo );
  3. }
  4. $ ("P"). BIND ("click", {FOO: "bar"}, Handler)


Copy code


Description: false is returned to cancel the default action and prevent event bubbles.

  1. $ ("Form"). BIND ("Submit", function () {return false ;})


Copy code


Description: only the default action is canceled by using the preventdefault () method.

  1. $ ("Form"). BIND ("Submit", function (event ){
  2. Event. preventdefault ();
  3. });


Copy code


Description: You can use the stoppropagation () method to stop only one event bubble.

  1. $ ("Form"). BIND ("Submit", function (event ){
  2. Event. stoppropagation ();
  3. });


Copy code


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: info-contact@alibabacloud.com 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.