2011-08-31 74 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

相关问题