Maison > Article > interface Web > Explication détaillée de la fonction bind() dans jQuery
La fonction
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>相信很多接触过jQuery的小伙伴对bind()函数一点都不陌生吧,但是很多小伙伴还不是很清bind()的含义与使用,bind()</span>
est utilisée pour lier un ou plusieurs événements pour chaque élément correspondant. La fonction de traitement des événements Aujourd'hui, nous allons vous présenter en détail la fonction bind() dans jQuery. .Explication détaillée!
De plus, vous pouvez également transmettre certaines données supplémentaires requises à la fonction de gestionnaire d'événements. Lorsque
exécute bind()
, le gestionnaire d'événements sera lié à chaque élément correspondant. Par conséquent, vous utilisez bind()
pour lier les événements de clic à tous les éléments de bouton. Vous liez les événements de clic à chaque élément de bouton qui existe dans le document à ce moment-là. Si vous ajoutez ultérieurement un nouvel élément bouton au document, l'événement lié ne prendra pas effet sur celui-ci. Si vous souhaitez que l'événement de liaison prenne également effet sur les éléments nouvellement ajoutés à l'avenir, veuillez utiliser des fonctions d'événement telles que on(), délégué(), live() (essayez d'abord d'utiliser les fonctions d'événement précédentes).
De plus, vous pouvez lier plusieurs gestionnaires d'événements au même élément et au même type d'événement. Lorsqu'un événement est déclenché, jQuery exécutera les fonctions de traitement des événements liés dans l'ordre de liaison.
Pour supprimer un événement lié via bind()
, utilisez la fonction unbind().
Cette fonction appartient à l'objet jQuery
(instance).
bind()
La fonction a principalement les deux formes d'utilisation suivantes :
Utilisation 1 :
jQueryObject.bind( events [, data ], handler )
Utilisation 2 : jQuery 1.4.3 prend désormais en charge cette utilisation.
jQueryObject.bind( events [, data ] [, isDefaultBubble ] )
Utilisation trois : jQuery 1.4 prend désormais en charge cette utilisation.
jQueryObject.bind( eventsMap )
Paramètre | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
événements |
|
||||||||||||
données | Facultatif/Tout type de données qui doivent être transmises à la fonction de traitement d'événements via event.data lors du déclenchement un événement. | ||||||||||||
gestionnaire | Fonction de traitement d'événements spécifiée par type de fonction. | ||||||||||||
isDefaultBubble | Le type facultatif/booléen spécifie s'il faut autoriser le comportement par défaut de l'élément et le bouillonnement d'événements lorsqu'un événement est déclenché. La valeur par défaut est true . |
||||||||||||
eventsMap | Objet de type un Objet Object , chacun de ses attributs correspond au type d'événement et à l'espace de noms facultatif (paramètre events ), et la valeur de l'attribut correspond à la fonction de traitement d'événement liée ( paramètre gestionnaire ).
|
关于参数events
中可选的命名空间(1.4.3+才支持),请参考最下面的示例代码。
参数handler
中的this
指向当前DOM元素。bind()
还会为handler
传入一个参数:表示当前事件的Event对象。
参数handler
的返回值与DOM原生事件的处理函数返回值作用一致。例如submit(表单提交)事件的事件处理函数返回false
,可以阻止表单的提交。
参数isDefaultBubble
用于指示是否允许元素的默认行为和事件冒泡。如果该值为false
,它将阻止元素的默认行为(例如:触发click事件的链接的默认跳转行为,触发submit事件的表单的默认提交行为)和事件的冒泡传递。
bind()
函数的返回值为jQuery类型,返回当前jQuery对象本身。
以点击事件("click")为例,以下是jQuery中事件函数的常规用法(某些函数也存在其它形式的用法,此处暂不列出):
<span class="com">// 这里的选择器selector用于指定可以触发事件的元素<span class="pln"><br/>// 这里的选择器ancestor应是selector的祖辈元素,selector触发的事件可以被其祖辈元素在事件流中捕获,从而以"委托代理"的形式触发事件。<span class="pln"><br/><br/>// jQuery 1.0+ (1.4.3+支持参数data)<span class="pln"><br/>$<span class="pun">(<span class="str">"selector"<span class="pun">).<span class="pln">click<span class="pun">(<span class="pln"> <span class="pun">[<span class="pln"> data <span class="pun">,]<span class="pln"> handler <span class="pun">);<span class="pln"><br/><br/>// jQuery 1.0+ (1.4.3+支持参数data)<span class="pln"><br/>$<span class="pun">(<span class="str">"selector"<span class="pun">).<span class="pln">bind<span class="pun">(<span class="pln"> <span class="str">"click"<span class="pln"> <span class="pun">[,<span class="pln"> data <span class="pun">],<span class="pln"> handler <span class="pun">);<span class="pln"><br/><br/>// jQuery 1.3+ (1.4+支持参数data)<span class="pln"><br/>$<span class="pun">(<span class="str">"selector"<span class="pun">).<span class="pln">live<span class="pun">(<span class="pln"> <span class="str">"click"<span class="pln"> <span class="pun">[,<span class="pln"> data <span class="pun">],<span class="pln"> handler <span class="pun">);<span class="pln"><br/><br/>// jQuery 1.4.2+<span class="pln"><br/>$<span class="pun">(<span class="str">"ancestor"<span class="pun">).<span class="pln">delegate<span class="pun">(<span class="pln"> <span class="str">"selector"<span class="pun">,<span class="pln"> <span class="str">"click"<span class="pln"> <span class="pun">[,<span class="pln"> data <span class="pun">],<span class="pln"> handler <span class="pun">);<span class="pln"><br/><br/>// jQuery 1.7+<span class="pln"><br/>$<span class="pun">(<span class="str">"ancestor"<span class="pun">).<span class="pln">on<span class="pun">(<span class="pln"> <span class="str">"click"<span class="pun">,<span class="pln"> <span class="str">"selector"<span class="pln"> <span class="pun">[,<span class="pln"> data <span class="pun">],<span class="pln"> handler <span class="pun">);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
请参考下面这段初始HTML代码:
<span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n1"<span class="tag">><span class="pln"><br/> <span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n2"<span class="tag">><span><span class="pln">CodePlayer<span class="tag"></span></p><span class="pln"><br/> <span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n3"<span class="tag">><span><span class="pln">专注于编程开发技术分享<span class="tag"></span></p><span class="pln"><br/> <span class="tag"><em<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n4"<span class="tag">><span class="pln">http://www.365mini.com<span class="tag"></em><span class="pln"><br/></p><span class="pln"><br/><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n5"<span class="tag">><span class="pln">Google<span class="tag"></p></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
我们为e388a4556c0f65e1904146cc1a846bee中的所有e388a4556c0f65e1904146cc1a846bee元素绑定点击事件:
<span class="com">//为p中的所有p元素的click事件绑定事件处理函数<span class="pln"><br/>//只有n2、n3可以触发该事件<span class="pln"><br/>$<span class="pun">(<span class="str">"p p"<span class="pun">).<span class="pln">bind<span class="pun">(<span class="str">"click"<span class="pun">,<span class="pln"> <span class="kwd">function<span class="pun">(){<span class="pln"><br/>// 这里的this指向触发点击事件的p元素(Element)<span class="pln"><br/> alert<span class="pun">(<span class="pln"> $<span class="pun">(<span class="kwd">this<span class="pun">).<span class="pln">text<span class="pun">()<span class="pln"> <span class="pun">);<span class="pln"><br/>});</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
运行代码(以下代码请自行复制到演示页面运行)
此外,我们还可以同时绑定多个事件,并为事件处理函数传递一些附加的数据,我们可以通过jQuery为事件处理函数传入的参数event
(Event事件对象)来进行处理:
<span class="kwd">var<span class="pln"> obj <span class="pun">=<span class="pln"> <span class="pun">{<span class="pln"> id<span class="pun">:<span class="pln"> <span class="lit">5<span class="pun">,<span class="pln"> name<span class="pun">:<span class="pln"> <span class="str">"王五"<span class="pln"> <span class="pun">};<span class="pln"><br/><br/>//为n5绑定mouseenter mouseleave两个事件,并为其传入附加数据obj<span class="pln"><br/>// 附加数据可以是任意类型<span class="pln"><br/>$<span class="pun">(<span class="str">"#n5"<span class="pun">).<span class="pln">bind<span class="pun">(<span class="str">"mouseenter mouseleave"<span class="pun">,<span class="pln"> obj<span class="pun">,<span class="pln"> <span class="kwd">function<span class="pun">(<span class="pln">event<span class="pun">){<span class="pln"><br/> <span class="kwd">var<span class="pln"> $me <span class="pun">=<span class="pln"> $<span class="pun">(<span class="kwd">this<span class="pun">);<span class="pln"><br/> <span class="kwd">var<span class="pln"> obj <span class="pun">=<span class="pln"> event<span class="pun">.<span class="pln">data<span class="pun">;<span class="pln"> <span class="com">// 这就是传入的附加数据<span class="pln"><br/> <span class="kwd">if<span class="pun">(<span class="pln"> event<span class="pun">.<span class="pln">type <span class="pun">==<span class="pln"> <span class="str">"mouseenter"<span class="pun">){<span class="pln"><br/> $me<span class="pun">.<span class="pln">html<span class="pun">(<span class="pln"> <span class="str">"你好,"<span class="pln"> <span class="pun">+<span class="pln"> obj<span class="pun">.<span class="pln">name <span class="pun">+<span class="pln"> <span class="str">"!"<span class="pun">);<span class="pln"><br/> <span class="pun">}<span class="kwd">else<span class="pln"> <span class="kwd">if<span class="pun">(<span class="pln">event<span class="pun">.<span class="pln">type <span class="pun">==<span class="pln"> <span class="str">"mouseleave"<span class="pln"> <span class="pun">){<span class="pln"><br/> $me<span class="pun">.<span class="pln">html<span class="pun">(<span class="pln"> <span class="str">"再见,"<span class="pln"> <span class="pun">+<span class="pln"> obj<span class="pun">.<span class="pln">name <span class="pun">+<span class="pln"> <span class="str">"!"<span class="pun">);<span class="pln"> <br/> <span class="pun">}<span class="pln"> <br/><span class="pun">}<span class="pln"> <span class="pun">);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
此外,如果符合条件的元素是bind()
函数执行后新添加的,绑定事件不会对其生效。同样以初始HTML代码为例,我们可以编写如下jQuery代码:
<span class="com">//为p中的所有p元素的click事件绑定事件处理函数<span class="pln"><br/>//只有n2、n3可以触发该事件<span class="pln"><br/>$<span class="pun">(<span class="str">"p p"<span class="pun">).<span class="pln">bind<span class="pun">(<span class="str">"click"<span class="pun">,<span class="pln"> <span class="kwd">function<span class="pun">(<span class="pln">event<span class="pun">){<span class="pln"><br/> alert<span class="pun">(<span class="pln"> $<span class="pun">(<span class="kwd">this<span class="pun">).<span class="pln">text<span class="pun">()<span class="pln"> <span class="pun">);<span class="pln"><br/>});<span class="pln"><br/><br/>// 新添加的n6不会触发上述click事件<span class="pln"><br/>$<span class="pun">(<span class="str">"#n1"<span class="pun">).<span class="pln">append<span class="pun">(<span class="str">'<p id="n6">上述绑定的click事件不会对该元素也生效!</p>'<span class="pun">);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
参数events
还支持为事件类型附加额外的命名空间。当为同一元素绑定多个相同类型的事件处理函数时。使用命名空间,可以在触发事件、移除事件时限定触发或移除的范围。
<span class="kwd">function<span class="pln"> clickHandler<span class="pun">(<span class="pln">event<span class="pun">){<span class="pln"><br/> alert<span class="pun">(<span class="pln"> <span class="str">"触发时的命名空间:["<span class="pln"> <span class="pun">+<span class="pln"> event<span class="pun">.<span class="pln">namespace <span class="pun">+<span class="pln"> <span class="str">"]"<span class="pun">);<span class="pln"><br/>}<span class="pln"><br/><br/>var<span class="pln"> $p <span class="pun">=<span class="pln"> $<span class="pun">(<span class="str">"p"<span class="pun">);<span class="pln"><br/><br/>// A:为所有p元素绑定click事件,定义在foo和bar两个命名空间下<span class="pln"><br/>$p<span class="pun">.<span class="pln">bind<span class="pun">(<span class="pln"> <span class="str">"click.foo.bar"<span class="pun">,<span class="pln"> clickHandler <span class="pun">);<span class="pln"><br/><br/>// B:为所有p元素绑定click事件,定义在test命名空间下<span class="pln"><br/>$p<span class="pun">.<span class="pln">bind<span class="pun">(<span class="pln"> <span class="str">"click.test"<span class="pun">,<span class="pln"> clickHandler <span class="pun">);<span class="pln"><br/><br/>var<span class="pln"> $n2 <span class="pun">=<span class="pln"> $<span class="pun">(<span class="str">"#n2"<span class="pun">);<span class="pln"><br/><br/>// 触发所有click事件<span class="pln"><br/>$n2<span class="pun">.<span class="pln">trigger<span class="pun">(<span class="str">"click"<span class="pun">);<span class="pln"> <span class="com">// 触发A和B (event.namespace = "")<span class="pln"><br/><br/>// 触发定义在foo命名空间下的click事件<span class="pln"><br/>$n2<span class="pun">.<span class="pln">trigger<span class="pun">(<span class="str">"click.foo"<span class="pun">);<span class="pln"> <span class="com">// 触发A (event.namespace = "foo")<span class="pln"><br/>// 触发定义在bar命名空间下的click事件<span class="pln"><br/>$n2<span class="pun">.<span class="pln">trigger<span class="pun">(<span class="str">"click.bar"<span class="pun">);<span class="pln"> <span class="com">// 触发A (event.namespace = "bar")<span class="pln"><br/>// 触发同时定义在foo和bar两个命名空间下的click事件<span class="pln"><br/>$n2<span class="pun">.<span class="pln">trigger<span class="pun">(<span class="str">"click.foo.bar"<span class="pun">);<span class="pln"> <span class="com">// 触发A (event.namespace = "bar.foo")<span class="pln"><br/><br/>// 触发定义在test命名空间下的click事件<span class="pln"><br/>$n2<span class="pun">.<span class="pln">trigger<span class="pun">(<span class="str">"click.test"<span class="pun">);<span class="pln"> <span class="com">// 触发B (event.namespace = "test")</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
bind()
函数的参数eventsMap
是一个对象,可以"属性-值"的方式指定多个"事件类型-处理函数"。对应的示例代码如下:
var eventsMap = { "mouseenter": function(event){ $(this).html( "Hello!"); }, "mouseleave": function(event){ $(this).html( "Bye!"); } }; //为n5绑定mouseenter mouseleave两个事件 $("#n5").bind( eventsMap );
总结:
本文使用示例代码详细的为大家分析了jQuery中关于bind()函数,相信大家对此也是有了进一步的了解,希望对你的工作有所帮助!
相关推荐:
Js的this指向 apply().call(),bind()的问题
带你了解JQuery中绑定事件(bind())和移除事件(unbind())
jQuery中 trigger() & bind() 使用心得
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!