Heim  >  Artikel  >  Web-Frontend  >  Detaillierte Erklärung der bind()-Funktion in jQuery

Detaillierte Erklärung der bind()-Funktion in jQuery

黄舟
黄舟Original
2017-12-06 14:13:153351Durchsuche

Die Funktion

<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>相信很多接触过jQuery的小伙伴对bind()函数一点都不陌生吧,但是很多小伙伴还不是很清bind()的含义与使用,bind()</span> wird verwendet, um ein oder mehrere Ereignisse für jedes passende Element zu binden. Die Funktion Ereignisverarbeitung Heute stellen wir Ihnen die Funktion bind() in jQuery im Detail vor . Ausführliche Erklärung!

Darüber hinaus können Sie auch einige zusätzliche erforderliche Daten an die Event-Handler-Funktion übergeben. Wenn

bind() ausführt, wird der Ereignishandler an jedes passende Element gebunden. Daher verwenden Sie bind(), um Klickereignisse an alle Schaltflächenelemente zu binden. Sie binden Klickereignisse an jedes Schaltflächenelement, das zu diesem Zeitpunkt im Dokument vorhanden ist. Wenn Sie dem Dokument später ein neues Schaltflächenelement hinzufügen, wird das gebundene Ereignis darauf nicht wirksam. Wenn Sie möchten, dass das Bindungsereignis in Zukunft auch auf neu hinzugefügte Elemente wirksam wird, verwenden Sie bitte Ereignisfunktionen wie on(), delegate(), live() (versuchen Sie zunächst, die früheren Ereignisfunktionen zu verwenden).

Darüber hinaus können Sie mehrere Event-Handler an dasselbe Element und denselben Event-Typ binden. Wenn ein Ereignis ausgelöst wird, führt jQuery die gebundenen Ereignisverarbeitungsfunktionen in der Reihenfolge der Bindung aus.

Um ein über bind() gebundenes Ereignis zu löschen, verwenden Sie die Funktion unbind().

Diese Funktion gehört zum jQuery-Objekt (Instanz).

Grammatik

bind()Die Funktion hat hauptsächlich die folgenden zwei Verwendungsformen:

Verwendung 1:

jQueryObject.bind( events [, data ], handler )

Verwendung 2: jQuery 1.4.3 unterstützt diese Verwendung neu.

jQueryObject.bind( events [, data ] [, isDefaultBubble ] )

Verwendung drei : jQuery 1.4 unterstützt diese Verwendung neu.

jQueryObject.bind( eventsMap )

Parameter

Parameter Beschreibung
events
参数 描述
events String类型一个或多个用空格分隔的事件类型和可选的命名空间,例如"click"、"focus click"、"keydown.myPlugin"。
data 可选/任意类型触发事件时,需要通过event.data传递给事件处理函数的任意数据。
handler Function类型指定的事件处理函数。
isDefaultBubble 可选/Boolean类型指定触发事件时,是否允许元素的默认行为和事件冒泡。默认为true
eventsMap Object类型一个Object对象,其每个属性对应事件类型和可选的命名空间(参数events),属性值对应绑定的事件处理函数(参数handler)。
String-Typ ein oder mehrere Ereignistypen, getrennt durch Leerzeichen und optional Namespace, wie „click“, „focus click“, „keydown.myPlugin“.
data Optional/Jeder Datentyp, der beim Auslösen über event.data an die Ereignisverarbeitungsfunktion übergeben werden muss ein Ereignis.
handler Ereignisbehandlungsfunktion, angegeben durch den Funktionstyp.
isDefaultBubble Der optionale/boolesche Typ gibt an, ob das Standardverhalten des Elements und die Ereignissprudelung zugelassen werden sollen, wenn ein Ereignis ausgelöst wird. Der Standardwert ist true.
eventsMap Objekttyp eins Objektobjekt , jedes seiner Attribute entspricht dem Ereignistyp und dem optionalen Namespace (Parameter events), und der Attributwert entspricht der gebundenen Ereignisverarbeitungsfunktion ( Parameter handler).

关于参数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">&#39;<p id="n6">上述绑定的click事件不会对该元素也生效!</p>&#39;<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() 使用心得

带你深入了解JQuery中Bind()函数

Das obige ist der detaillierte Inhalt vonDetaillierte Erklärung der bind()-Funktion in jQuery. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn