” , 그러나 많은 친구들은 여전히 바인딩(), 바인딩()
함수 이벤트 처리 함수, 오늘과 일치하는 각 요소에 대해 하나 이상의 이벤트를 바인딩하는 데 사용됩니다. jQuery의 바인딩() 함수에 대해 자세히 소개하겠습니다! <span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>相信很多接触过jQuery的小伙伴对bind()函数一点都不陌生吧,但是很多小伙伴还不是很清bind()的含义与使用,bind()</span>
函数用于为每个匹配元素的一个或多个事件绑定事件处理函数、今天我们就带大家详细介绍下jQuery中关于bind()函数详解!
此外,你还可以额外传递给事件处理函数一些所需的数据。
执行bind()
时,事件处理函数会绑定到每个匹配元素上。因此你使用bind()
所有button元素绑定了click事件,是为当时文档中存在的每个button元素绑定click事件。如果之后你向文档中添加了新的button元素,绑定事件不会对其生效。如果你希望绑定事件对未来新添加的元素也生效,请使用on()、delegate()、live()等事件函数(尽量优先使用靠前的事件函数)。
此外,你可以为同一元素、同一事件类型绑定多个事件处理函数。触发事件时,jQuery会按照绑定的先后顺序依次执行绑定的事件处理函数。
要删除通过bind()
绑定的事件,请使用unbind()函数。
该函数属于jQuery
对象(实例)。
bind()
函数主要有以下两种形式的用法:
用法一:
jQueryObject.bind( events [, data ], handler )
用法二:jQuery 1.4.3 新增支持该用法。
jQueryObject.bind( events [, data ] [, isDefaultBubble ] )
用法三:jQuery 1.4 新增支持该用法。
jQueryObject.bind( eventsMap )
参数 | 描述 |
---|---|
events | String类型一个或多个用空格分隔的事件类型和可选的命名空间,例如"click"、"focus click"、"keydown.myPlugin"。 |
data | 可选/任意类型触发事件时,需要通过event.data传递给事件处理函数的任意数据。 |
handler | Function类型指定的事件处理函数。 |
isDefaultBubble | 可选/Boolean类型指定触发事件时,是否允许元素的默认行为和事件冒泡。默认为true 。 |
eventsMap |
Object类型一个Object对象,其每个属性对应事件类型和可选的命名空间(参数events ),属性值对应绑定的事件处理函数(参数handler 또한 일부 추가 필수 데이터를 이벤트 핸들러 함수에 전달할 수도 있습니다. |
jQuery
개체(인스턴스)에 속합니다. bind()
이 함수는 주로 다음과 같은 두 가지 형태의 사용법을 갖습니다:🎜용법 1: p><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>🎜사용법 2: jQuery 1.4.3에서는 이 사용법을 새로 지원합니다. 🎜
<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>🎜사용법 3: jQuery 1.4에서는 이 사용 방법을 새로 지원합니다. 🎜
<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>
매개변수 | 설명 | 🎜||||||||
---|---|---|---|---|---|---|---|---|---|
events🎜 | 문자열 유형 공백으로 구분된 하나 이상의 이벤트 유형과 선택 사항 네임스페이스(예: "click", "focus click", "keydown.myPlugin") 🎜🎜🎜data🎜 |
선택/임의 유형 이벤트가 트리거되면 모든 데이터는 event.data를 통해 이벤트 처리 기능으로 전달되어야 합니다. 🎜🎜🎜 |
handler🎜 |
함수 유형에 따라 지정되는 이벤트 처리 함수입니다. 🎜🎜🎜 |
isDefaultBubble🎜 |
Optional/Boolean 유형은 이벤트가 트리거될 때 요소의 기본 동작과 이벤트 버블링을 허용할지 여부를 지정합니다. 기본값은 |
true 입니다. 🎜🎜🎜eventsMap🎜 |
객체 유형 1객체 객체의 각 속성은 이벤트 유형 및 선택적 네임스페이스(매개변수 |
events )에 해당하고 속성 값은 바인딩된 이벤트 처리 함수(매개변수 handler)에 해당합니다. ). 🎜🎜🎜🎜🎜<p>关于参数<code>events 中可选的命名空间(1.4.3+才支持),请参考最下面的示例代码。
参数 参数 参数 返回值
示例&说明以点击事件("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为事件处理函数传入的参数 <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> 此外,如果符合条件的元素是 <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> 参数 <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>
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의 바인딩() 함수에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!