简体   繁体   English

绑定和取消绑定在jquery中意味着什么?

[英]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 将click事件处理程序绑定到id为divElement的元素

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

unbinds a click event handler to the element with id divElement 将click事件处理程序解除绑定到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. 在jQuery 1.7中, 。对().off()方法是优选的附加和上元件移除事件处理程序。

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) . 根据您要处理的事件类型,您可以使用不同的功能,例如click(function) (alt: bind('click', function)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. unbind('click', function)取消绑定某个处理程序, unbind('click')取消绑定所有click处理程序, unbind()取消绑定所有处理程序。 You can substitute click for other types of events of course. 当然,您可以将click替换为其他类型的事件。

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). 绑定附加一段代码以运行给定的HTML元素(在提供的事件上运行)。 unbind removes it. unbind删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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