2011-08-31 73 views
0

繼承人的的jsfiddle,jsfiddle.net/kqreJ正在使用.bind,但現在已經使用.delegate ...已嘗試.undelegate?

所以我是用.bind這個功能沒有問題,但後來我裝更多的更新的網頁,並發現.bind不進口的網頁內容的工作,但只是爲了網頁上的內容!大!

所以我切換它到.delegate這是很酷,但現在我無法弄清楚如何.bind .unbind我的函數它的方式???

使用.bind功能,其工作完美......除了Ajax的內容沒有工作.. :(

$('.open').bind("mouseup",function(event) { 
var $this = $(this), handler = arguments.callee; 
$this.unbind('mouseup', handler); 
var id = $(this).attr("id"); 
var create = 'nope'; 

    var regex = /\d+$/, 
    statusId = $('#maindiv .open').toArray().map(function(e){ 
     return parseInt(e.id.match(regex)); 
    }); 

var divsToCreate = [ parseInt(id) ]; 

$.each(divsToCreate, function(i,e) 
{ 
    if ($.inArray(e, statusId) == -1) { 
     create = 'yup'; 
    } 
}); 

     if(create == 'yup') { 
      if(id) { 
        $.ajax({ 
        type: "POST", 
        url: "../includes/open.php", 
        data: "post="+ id, 
        cache: false, 
        success: function(html) { 
        $('.open').html(html); 
        $this.click(handler); 
        } 
        }); 
      } 
     } 

}); 

使用未綁定,並創建多個實例.delegate新功能?

$('#maindiv').delegate("span.open", "mouseup",function(event) { 
var $this = $(this), handler = arguments.callee; 
$this.unbind('mouseup', handler); 
var id = $(this).attr("id"); 
var create = 'nope'; 

    var regex = /\d+$/, 
    statusId = $('#maindiv .open').toArray().map(function(e){ 
     return parseInt(e.id.match(regex)); 
    }); 

var divsToCreate = [ parseInt(id) ]; 

$.each(divsToCreate, function(i,e) 
{ 
    if ($.inArray(e, statusId) == -1) { 
     create = 'yup'; 
    } 
}); 

     if(create == 'yup') { 
      if(id) { 
        $.ajax({ 
        type: "POST", 
        url: "../includes/open.php", 
        data: "post="+ id, 
        cache: false, 
        success: function(html) { 
        $('.open').html(html); 
        $this.click(handler); 
        } 
        }); 
      } 
     } 

}); 

我已經花了幾個小時嘗試,因爲我想學習如何做自己想出解決辦法,但我不得不打破尋求幫助......感到沮喪!

我也讀到,當你綁定,你必須解除綁定.delegate把它的Ajax內容上面?我試過使用.die()和.undelegate()...也許我只是不知道在哪裏放置它?

回答

1

undelegate

它確實需要一看就delegate什麼unbind確實給bind

在你的情況,我認爲它會是這樣的:

$('#maindiv').undelegate("span.open", "mouseup").delegate("span.open", "mouseup" ... 

然後你就可以在函數中刪除$this.unbind('mouseup', handler);

+0

那豈不是去哪兒$ this.click(處理);是什麼? $('#maindiv')。undelegate(「。open」,「mouseup」); – Brandon

+0

有罪未仔細查看代碼!但是你不必在evry ajax調用成功的時候這樣做。在dom做好一次準備,並且通過ajax添加的任何新內容都將正確連接。我需要把你的代碼和格式化一點以便能夠查明,但你明白了。 – Mrchief

+0

先生,我必須放置$ this.unbind('mouseup',處理程序);在ajax調用之上或同一個地方,我有$ this.click(handler);當功能完善工作用.bind(「鼠標鬆開... – Brandon

相關問題