2013-09-24 56 views
-1


我遇到過一個場景。我有一些具有相同類的元素。所有人都點擊相同的事件處理程序。如果某些條件成立,我需要從特定元素中解除綁定。
這是我的片段
取消綁定單擊類中的特定元素

 $(".del_grp").die().live('click', deleteGroup); 
         $(".del_grp").css('opacity','1'); 
         $(".del_grp").css('cursor','pointer'); 
         if($('.map_unit').length === 1){ 
          $('.grp').each(function(){ 

           if($(this).next().children().length !==0){ 
            $(this).find('.del_grp').die('click'); 
            $(this).find('.del_grp').css('opacity','0.5'); 
            $(this).find('.del_grp').css('cursor','default'); 
           } 
          }); 
         } 

這裏,CSS性能都正常工作時,該die()不工作

+1

的文檔演示說'注意事項:爲了.die()正常工作,它使用的選擇必須首先使用.live選擇完全匹配()。 ' – TCHdvlp

+0

從jQuery 1.7開始,不建議使用.die()(及其補充方法,.live())。相反,使用.off()去除與.on()綁定的事件處理程序 - [文檔jQuery](http://api.jquery.com/die/) – aldanux

+1

我會檢查你使用的是什麼版本的jquery, '.live()'和'.die()'不推薦使用。 '.on()'和'.off()'是替代品。 – Rchristiani

回答

0

使用.off(),而不是.die().on()代替.live()。它正常工作。見下面

var deleteGroup = function() { 
    console.log('del group clicked'); 
}; 
$(".del_grp").off().on('click', deleteGroup); 
$(".del_grp").css('opacity','1'); 
$(".del_grp").css('cursor','pointer'); 
if($('.map_unit').length === 1){ 
    $('.grp').each(function(){ 
     console.log('for each group'); 
     if($(this).next().children().length !==0){ 
      $(this).find('.del_grp').off('click', deleteGroup); 
      $(this).find('.del_grp').css('opacity','0.5'); 
      $(this).find('.del_grp').css('cursor','default'); 
     } 
    }); 
} 

http://jsfiddle.net/ChaitanyaMunipalle/xz8Up/

0

嘗試

$("#foo").unbind("click");