首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >整个数组中的jQuery解除绑定事件

问整个数组中的jQuery解除绑定事件
EN

Stack Overflow用户
提问于 2011-04-27 19:46:58
回答 1查看 444关注 0票数 0

大家好,有人知道jquery unbind函数是如何工作的吗?我有一个由jquery驱动的图片库,我在这里绑定了一些关于加载事件(改变图像等)的东西,当用户在图库中快速浏览时,我想要取消绑定“旧的”事件,然后只在最后一个图像上绑定..But事件。否则用户将看到他浏览过的每个图像,而我只想显示最后一个..(性能,UX,..)我将每个加载的(不是完全加载的,也是每个请求的)图像存储在一个数组中,所以每次我调用我的loadImage函数时,我调用jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader"),但它似乎不起作用,加载事件仍然在那里(有点奇怪,我在调试器中找不到它,但我看到了它:)有没有方法从整个数组或整个js实例解除绑定事件,除非通过foreach?

这里有一些代码snippet..It是整个loadImage函数..

代码语言:javascript
复制
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);


            }


    } );

}

EN

回答 1

Stack Overflow用户

发布于 2011-04-27 20:38:42

你需要解绑not like

代码语言:javascript
复制
jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader")

但

代码语言:javascript
复制
jQuery(myArrayWithImages).unbind(load.backgroundImagesLoader).

只需阅读手册,您就可以使用名称空间:

代码语言:javascript
复制
$('#foo').bind('click.myEvents', handler);

和

代码语言:javascript
复制
$('#foo').unbind('click.myEvents');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5803551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档