jQuery unbind() 方法

來源:互聯網
上載者:User

jQuery 中的 unbind() 方法是 bind() 方法的反向操作,從每一個匹配的元素中刪除綁定的事件。

文法結構:

[javascript]
unbind([type][, data]); 
unbind([type][, data]);
type是事件類型,data為將要移除的事件。具體說明如下:

1、如果沒有參數,則刪除所有的綁定事件;

2、如果提供了事件類型(type)作為參數,則只刪除該類型的綁定事件;

3、如果把在綁定時傳遞的處理函數作為第2個參數,則只有這個特定的事件處理函數被刪除。

請看下面的舉例:

[javascript]
<script src="http://www.gamejzy.com/js/jquery.js" type="text/javascript"></script> 
<style> 
.info { 
    background:#ffff66; 

</style> 
<input type="button" id="btn" value="點擊我" /> <input type="button" id="delAll" value="刪除全部綁定函數" /> <input type="button" id="delFun2" value="刪除第二個綁定函數" /><br /> 
<div class="info"></div> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    // 為id為btn的按鈕添加綁定事件  
    $("#btn").bind('click', fun1=function(){ 
        $(".info").append('<p>綁定函數1</p>'); 
    }).bind('click', fun2=function(){ 
        $(".info").append('<p>綁定函數2</p>'); 
    }).bind('click', fun3=function(){ 
        $(".info").append('<p>綁定函數3</p>'); 
    }) 
    $("#delAll").bind('click', function(){ 
        $("#btn").unbind(); //刪除全部綁定事件  
    }) 
    $("#delFun2").bind('click', function(){ 
        $("#btn").unbind('click', fun2);  //刪除第二個綁定函數  
    }) 
}) 
</script> 
<script src="http://www.gamejzy.com/js/jquery.js" type="text/javascript"></script>
<style>
.info {
 background:#ffff66;
}
</style>
<input type="button" id="btn" value="點擊我" /> <input type="button" id="delAll" value="刪除全部綁定函數" /> <input type="button" id="delFun2" value="刪除第二個綁定函數" /><br />
<div class="info"></div>
<script type="text/javascript">
$(document).ready(function(){
 // 為id為btn的按鈕添加綁定事件
 $("#btn").bind('click', fun1=function(){
  $(".info").append('<p>綁定函數1</p>');
 }).bind('click', fun2=function(){
  $(".info").append('<p>綁定函數2</p>');
 }).bind('click', fun3=function(){
  $(".info").append('<p>綁定函數3</p>');
 })
 $("#delAll").bind('click', function(){
  $("#btn").unbind(); //刪除全部綁定事件
 })
 $("#delFun2").bind('click', function(){
  $("#btn").unbind('click', fun2);  //刪除第二個綁定函數
 })
})
</script>
效果展示圖:

 
作者:liruxing1715
 
 

聯繫我們

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