Sivo 悉见

JQuery移除事件

匿名 · 更新于 2013/12/23

移除事件

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; unbind(type [,data])&nbsp;&nbsp;&nbsp;&nbsp; //data是要移除的函数</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).unbind(&quot;click&quot;); //移除click</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).unbind(); //移除所有 </span><br />
		<span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 对于只需要触发一次的,随后就要立即解除绑定的情况,用one()</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).one(&quot;click&quot;,function(){.......}); </span><br />
		<span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 模拟操作</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 可以用trigger()方法完成模拟操作。</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).trigger(&quot;click&quot;); </span><br />
		<span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).click();</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 触发自定义事件</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).bind(&quot;myclick&quot;,function(){....});</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).trigger(&quot;myclick&quot;); </span><br />
		<span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 传递数据</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; trigger(type [,data])</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).bind(&quot;myclick&quot;,function(event,message1,message2){...........});</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&#39;#btn&#39;).trigger(&quot;myclick&quot;,[&quot;传给message1&quot;,&quot;传给message2&quot;]);</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 执行默认操作 </span><br />
		<span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&quot;input&quot;).trigger(&quot;focus&quot;);</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //不仅会触发input元素绑定的focus事件,还会触发默认操作&mdash;&mdash;得到焦点。</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&quot;input&quot;).triggerHandler(&quot;focus&quot;);</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //只触发绑定事件,不执行浏览器默认操作</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 其他用法</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 绑定多个事件类型</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&quot;div&quot;).bind(&quot;mouseover mouseout&quot;,function(){.....});</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 添加事件命名空间</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&quot;div&quot;).bind(&quot;click.plugin&quot;,function(){......});</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 在所绑定的世界类型后面添加命名空间,这样在删除事件时只需要指定命名空间即可。</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(&quot;div&quot;).unbind(&quot;.plugin&quot;);&nbsp;&nbsp; //删除空间内的事件</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; $(&quot;div&quot;).trigger(&quot;click!&quot;); //触发所以不包含在命名空间中的click方法</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp; 如果包含在命名空间的也要触发:</span></p>

		<p><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(&quot;div&quot;).trigger(&quot;click&quot;);</span></p>
		</div>
		</td>
	</tr>
</tbody>

另外摘录

================================================
$('div').bind('click', RecommandProduct);//为div绑定RecommandProduct 函数
$('div').unbind('click', RecommandProduct);//取消RecommandProduct 函数