jquery unbind刪除綁定事件

來源:互聯網
上載者:User

使用jquery的bind方法可以為一個對象的一個事件綁定綁定多個處理函數,甚至可以多次綁定一個方法,比如:

 代碼如下 複製代碼

$(xxx).bind(‘click’, fun1).bind(‘click’, fun2).bind(‘click’, fun1);

而unbind用於刪除綁定的方法,比如$(xxx).unbind(‘click’)

……

但是,如果只想刪除fun2哪?以前看得很多jquery裡都沒有關於這種操作的介紹,所以自然就一直以為只能整個的將click處理調,今天試了另外一種操作:$(xxx).unbind(‘click’, fun2),可以解除fun2的綁定

住:$(xxx).unbind(‘click’, fun1),會將之前註冊的兩次fun1全部刪除掉

刪除綁定事件

1.移除所有p元素的綁定事件:

 代碼如下 複製代碼

$("p").unbind();

2.移除p元素繫結的click事件:

 代碼如下 複製代碼

$("p").unbind("click");

$("p").unbind("click",funName);

注意:unbind方法只適用於取消jquery綁定的事件,如果直接使用document.getElementById("btn").onclick=function{//code...}或<div onclick="alert('c')">test</div>,不能通過unbind方法取消(刪除)。

移除標籤方法

 代碼如下 複製代碼
 
$("指定對象").remove();=>即可實現 
$("#menuTree").unbind();//移除所有的綁定事件 
 $("#menuTree").attr("class","scroll org_tree white"); 
 $("#menuTre ul").remove();//刪除樹 


補充:

在jQuery下使用 $(selector).unbind(‘hover’)無效,可以使用如下方法代替:

 代碼如下 複製代碼
$(selector).unbind(‘mouseenter’).unbind(‘mouseleave’);
或:
$(selector).unbind(‘mouseenter mouseleave’);

可以發現hover事件是由mouseenter和mouseleave組成,以區別mouseover和mouseout。
在unbind它的這兩個事件後,不會對mouseover和mouseout事件造成影響。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.