简体   繁体   中英

What does bind and unbind mean in jquery?

在愚蠢的慢慢学习者术语中,jquery中绑定和解除绑定意味着什么?

In simple terms: for binding and unbinding event handlers to elements.

$("#divElement").bind('click', functionName);

binds a click event handler to the element with id divElement

$("#divElement").unbind('click', functionName);

unbinds a click event handler to the element with id divElement

Edit:

Bind also allows you to bind a handler to one or more events.

$("#divElement").bind("click dblclick mouseout", function(){ // your code });

Update:

As of jQuery 1.7, the .on() and .off() methods are preferred to attach and remove event handlers on elements.

Binding: coupling an handler to an element (s), that will run when an event occurs on said element(s). Depending on what kind of event you want to handle you'd use different functions like click(function) (alt: bind('click', function) or focus(function) (alt: bind('focus', function) .

Unbinding: de-coupling of an handler from an element(s) , so that when an event occurs the handler function will no longer run. Unbinding is always the same; unbind('click', function) to unbind a certain handler, unbind('click') to unbind ALL click handlers, and unbind() to unbind ALL handlers. You can substitute click for other types of events of course.

In three sentences:

An event is a signal that is visible in your program - a key press, for example.

A handler is a function that is geared towards reacting to a certain event.

Binding associates a handler with an event, unbinding does the opposite.

Bind attaches a piece of code to be run to a given HTML element (which is run on the supplied event). unbind removes it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM