大家好,有人知道jquery unbind函数是如何工作的吗?我有一个由jquery驱动的图片库,我在这里绑定了一些关于加载事件(改变图像等)的东西,当用户在图库中快速浏览时,我想要取消绑定“旧的”事件,然后只在最后一个图像上绑定..But事件。否则用户将看到他浏览过的每个图像,而我只想显示最后一个..(性能,UX,..)我将每个加载的(不是完全加载的,也是每个请求的)图像存储在一个数组中,所以每次我调用我的loadImage函数时,我调用jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader"),但它似乎不起作用,加载事件仍然在那里(有点奇怪,我在调试器中找不到它,但我看到了它:)有没有方法从整个数组或整个js实例解除绑定事件,除非通过foreach?
这里有一些代码snippet..It是整个loadImage函数..
function loadImage(index , showLoading, callback){
if(backgroundImages==undefined) backgroundImages=Array();
//if(backgroundImages[window.location.hash][index]==undefined)backgroundImages[window.location.hash][index] = Array();
jQuery("#galleryEl .sig_thumb a").each( function( pos , el ) {
var wh_bg_b = getVierwportSize();
if(index == pos){
var bg_img_path_b = jQuery(el).attr('href');
var size = 'max';
if ( bg_img_path_b != undefined ) {
if ( (wh_bg_b[0] < 1281) && (wh_bg_b[1] < 801) ) {
size = 'min';
bg_img_path_b = bg_img_path_b.substring( 0, bg_img_path_b.lastIndexOf("/") ) + "/1280/" + bg_img_path_b.substring( bg_img_path_b.lastIndexOf("/")+1 );
} else if ( (wh_bg_b[0] < 1441) && (wh_bg_b[1] < 901) ) {
size = 'med';
bg_img_path_b = bg_img_path_b.substring( 0, bg_img_path_b.lastIndexOf("/") ) + "/1440/" + bg_img_path_b.substring( bg_img_path_b.lastIndexOf("/")+1 );
}
}
console.log("test");
if(backgroundImages[bg_img_path_b]!=undefined){
if(backgroundImages[bg_img_path_b].loaded=="true"){
//console.log(index+" "+backgroundImages[window.location.hash][index][size]['loaded']);
if(typeof callback=='function'){
callback.call(this, bg_img_path_b);
} }
else if(backgroundImages[bg_img_path_b].loaded=="loading"){
jQuery(backgroundImages).unbind('load.backgroundImages');
jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){
backgroundImages[bg_img_path_b].loaded="true";
if(typeof callback=='function'){
callback.call(this, bg_img_path_b);
//console.log("loaded "+index);
}
});
}
}
else{
backgroundImages[bg_img_path_b]=new Image();
backgroundImages[bg_img_path_b].src = bg_img_path_b;
backgroundImages[bg_img_path_b].loaded="loading";
jQuery(backgroundImages).unbind('load.backgroundImages');
jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){
backgroundImages[bg_img_path_b].loaded="true";
if(typeof callback=='function'){
callback.call(this, bg_img_path_b);
//console.log("loaded "+index);
}
});
}
console.log(backgroundImages[bg_img_path_b]);
//console.log(size);
//console.log(backgroundImages);
}
} );}
发布于 2011-04-27 20:38:42
你需要解绑not like
jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader")但
jQuery(myArrayWithImages).unbind(load.backgroundImagesLoader).只需阅读手册,您就可以使用名称空间:
$('#foo').bind('click.myEvents', handler);和
$('#foo').unbind('click.myEvents');https://stackoverflow.com/questions/5803551
复制相似问题