整个数组中的jQuery unbind事件

时间:2011-04-27 11:46:58

标签: javascript jquery load

嗨有没有人知道jquery unbind函数是如何工作的?我有一个jquery驱动的照相馆,我在加载事件上绑定了一些东西(更改图像等等)..但是当用户快速走过画廊时我想要取消绑定“较旧”事件,而不是仅在最后一个图像上绑定加载事件。否则,用户将看到他走过的每一个图像片刻,我只想显示最后一个...(性能,UX,..)我将每个加载的(不是完全加载的,每个请求的)图像存储在一个数组中,所以每当我调用我的loadImage函数时,我都会调用jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader") 但似乎它不起作用,加载事件仍然存在(它有点奇怪,我无法在调试器中找到它,但我看到它:)) 有没有办法解决整个数组或整个js实例中的事件,除非通过foreach进行解析?

这是一些代码片段..它是完整的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);


            }


    } );

}

1 个答案:

答案 0 :(得分:0)

你需要解开不喜欢

jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader")

但是

jQuery(myArrayWithImages).unbind(load.backgroundImagesLoader).

只需阅读手册,即可使用命名空间:

$('#foo').bind('click.myEvents', handler);

$('#foo').unbind('click.myEvents');