JQuery移除事件

來源:互聯網
上載者:User

標籤:資料   io   cti   div   c   type   

移除事件

    unbind(type [,data])     //data是要移除的函數

    $(‘#btn‘).unbind("click"); //移除click

    $(‘#btn‘).unbind(); //移除所有 
    對於只需要觸發一次的,隨後就要立即解除綁定的情況,用one()

    $(‘#btn‘).one("click",function(){.......}); 
    類比操作

    可以用trigger()方法完成類比操作。

    $(‘#btn‘).trigger("click"); 
    $(‘#btn‘).click();

    觸發自訂事件

    $(‘#btn‘).bind("myclick",function(){....});

    $(‘#btn‘).trigger("myclick"); 
    傳遞資料

    trigger(type [,data])

    $(‘#btn‘).bind("myclick",function(event,message1,message2){...........});

    $(‘#btn‘).trigger("myclick",["傳給message1","傳給message2"]);

    執行預設操作 
    $("input").trigger("focus");

        //不僅會觸發input元素繫結的focus事件,還會觸發預設操作——得到焦點。

    $("input").triggerHandler("focus");

        //只觸發綁定事件,不執行瀏覽器預設操作

    其他用法

    綁定多個事件類型

    $("div").bind("mouseover mouseout",function(){.....});

    添加事件命名空間

    $("div").bind("click.plugin",function(){......});

    在所綁定的世界類型後面添加命名空間,這樣在刪除事件時只需要指定命名空間即可。

        $("div").unbind(".plugin");   //刪除空間內的事件

    $("div").trigger("click!"); //觸發所以不包含在命名空間中的click方法

    如果包含在命名空間的也要觸發:

        $("div").trigger("click");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.