在回调内绑定/取消绑定
我遇到了来自垃圾浏览器的各种各样的问题,我似乎无法用我读过的任何东西来克服这些问题。情况是这样的...我正在使用第三方插件来生成图库。该插件带有图片更改回调,每次更改图像时都会触发该回调。
我想做的是为两个 div 添加 css...其中一个是包装 div,当单击子 div 时它将滑出,并在再次单击子 div 时滑回。
这是回调:
changepicturecallback: function(){
$('.sxpp_commentsImage').unbind(toggle);
oldWidth = $('.pp_pic_holder').width() - 20,
newWidth = $('.pp_pic_holder').width() + (200),
newHeight = $('.pp_hoverContainer').height(),
cImageAlign = newWidth - 20;
var toggle = function() {
if(newWidth == $('.sxpp_comments').width()){
$('.sxpp_comments').animate({width: oldWidth});
}else{
$('.sxpp_comments').animate({width: newWidth});
}
}
$('.sxpp_comments').width(oldWidth).css('background-color','red');
$('.sxpp_commentsImage').css({'float':'right','background-color':'green','width':'20px','height':newHeight});
$('.sxpp_commentsImage').bind('click',toggle);
}
这在 FF4 和 FF4 中工作得很好。 Chrome...但是,就好像 Internet Explorer 中没有发生“绑定”。我一直在阅读“.live”和“.live”。 “.delegate”方法,但(A)我不熟悉它们是如何工作的(B)如果可以清除更改上的“.live”,然后将其添加回来,就像我对绑定所做的那样。
有什么见解或我可能会忽略的事情吗?
编辑***...单击链接时会动态创建 div(显示灯箱)。所以从我读到的所有内容来看……绑定事件无论如何都是错误的路线。
I'm having all sorts of problems from the crap browser that I can't seem to overcome with anything I've read. Here's the situation... I'm using a third party plugin to generate a gallery. This plugin comes with a callback on picture change that fires each time the image is changed.
What i'm trying to do is add the css for two div's... one that is a wrapper div which will slide out when the child div is clicked and slide back in when the child is clicked again.
Here's the callback:
changepicturecallback: function(){
$('.sxpp_commentsImage').unbind(toggle);
oldWidth = $('.pp_pic_holder').width() - 20,
newWidth = $('.pp_pic_holder').width() + (200),
newHeight = $('.pp_hoverContainer').height(),
cImageAlign = newWidth - 20;
var toggle = function() {
if(newWidth == $('.sxpp_comments').width()){
$('.sxpp_comments').animate({width: oldWidth});
}else{
$('.sxpp_comments').animate({width: newWidth});
}
}
$('.sxpp_comments').width(oldWidth).css('background-color','red');
$('.sxpp_commentsImage').css({'float':'right','background-color':'green','width':'20px','height':newHeight});
$('.sxpp_commentsImage').bind('click',toggle);
}
This works perfectly fine in FF4 & Chrome...however, it's as if the "bind" isn't happening in Internet Explorer. I've been reading up on the ".live" & ".delegate" methods but (A) I'm not familiar with how they work (B) If it's possible to clear that ".live" on the change then add it back as I'm doing with the bind.
Any insight or things I might be overlooking??
Edit***... The divs are created dynamically (a lightbox is shown) when a link is clicked. So the from all I'm reading... the bind event is the wrong route to go anyhow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想在回调中取消绑定事件函数,只需使用以下命令:
If you want to unbind event funciton inside it's callback just use this:
bind()
用于页面加载时存在的元素。适合静态。live()
是动态生成元素的唯一解决方案。动态的唯一选择。您应该取消绑定诸如
click
或toggle
之类的事件,而不是实际的函数名称,这可能是您的问题。bind()
is for elements which are there when the page loads. Good for static.live()
is the only solution for dynamically generated elements. Only choice for dynamic.You should unbind events like
click
ortoggle
, not actual function names, which might be your problem.尝试
而不是
编辑:
或使用:
然后
在取消绑定时不会声明切换。
try
instead of
Edit:
Or use:
and then
as toggle would not be declared by the time you do the unbind.