问题描述
此处是jsfiddle, jsfiddle.net/kqreJ
Heres the jsfiddle, jsfiddle.net/kqreJ
因此,我对该功能使用.bind没问题,但随后我在页面上加载了更多更新,发现.bind不适用于导入到页面的内容,而仅适用于页面上已有的内容!太好了!
So I was using .bind no problem for this function but then I loaded more updates to the page and found out that .bind doesn't work for content imported to the page but just for content already on the page! Great!
所以我将其切换到.delegate,这很酷,但是现在我不知道如何.bind.以原来的方式取消绑定我的功能了?
So I switched it up to .delegate which is pretty cool but now I can't figure out how to .bind .unbind my function the way it was???
使用.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);
}
});
}
}
});
我花了数小时试图解决这个问题,因为我喜欢自己学习如何做,但是我不得不崩溃并寻求帮助……沮丧!
I've spent hours trying to figure this out because I like learning how to do it myself but I had to break down and ask for help... getting frustrated!
我还读到,当您进行绑定和取消绑定.delegate时,必须将其置于ajax内容之上吗?我尝试使用.die()和.undelegate()...也许我只是不知道将其放置在哪里?
I also read that when your binding and unbinding .delegate you have to put it above the ajax content? I've tried using .die() and .undelegate()... Maybe I just don't know where to place it?
推荐答案
看看 undelegate
它对delegate
的作用与unbind
对bind
的作用一样.
It does to delegate
what unbind
does to bind
.
在您的情况下,我认为应该是这样的:
In your case, I think it'd be something like:
$('#maindiv').undelegate("span.open", "mouseup").delegate("span.open", "mouseup" ...
然后您可以将$this.unbind('mouseup', handler);
放到函数中.
Then you can drop the $this.unbind('mouseup', handler);
within the function.
这篇关于正在使用.bind但现在不得不使用.delegate ...尝试过.undelegate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!