简体   繁体   English

jQuery悬停效果在Ajax调用后停止工作

[英]Jquery hover effects stop working after ajax call

In my website there are some ajax calls that filter product results. 在我的网站中,有一些ajax调用可以过滤产品结果。 There are some effects applied on these products, which stop working after ajax call completes. 这些产品上有一些影响,这些影响会在ajax调用完成后停止工作。

At the bottom of the page's code, there's the following: 在页面代码的底部,有以下内容:

jQuery(function($) {

    jQuery('.category-products-grid').on('mouseenter', '.item', function() {
        ...
        ...
        ...
    }).on('mouseleave', '.item', function() {
        ...
        ...
        ...
    });

});

I've seen lots of similar posts about this issue, but none of the solutions suggested worked for me. 我已经看到过很多关于此问题的类似文章,但所建议的解决方案均不适合我。

UPDATE Here is the ajax call code. 更新这是ajax调用代码。 i didn't posted before because I don't believe it provides helpful information. 我以前没有发布过,因为我不认为它可以提供有用的信息。 There are many functions called int it and the code is too big to post here. 有许多称为int it的函数,并且代码太大,无法在此处发布。 The function that matters here is replaceProductsBlock(), and so I'll post it here: 这里重要的函数是replaceProductsBlock(),因此我将其发布在这里:

var request = new Ajax.Request(url,
{
method:'GET',
parameters:params,
onSuccess: function(transport){

    ....

    replaceProductsBlock(response);

    ....

},
onFailure: function(){
    ....
}
});




replaceProductsBlock: function(response, need_scroll){

    var content = response.product_list;

    if (typeof(this.gan_static_navigation_url) != 'undefined' && this.gan_static_navigation_url){
        if ($$('div.col-main').length > 0){
            var col_main = $$('div.col-main')[0];
            col_main.innerHTML += '<div class="category-view">' + content + '</div>';
        }
        return;
    }

    var replace_toolbar = false;

    if($$('div.category-products').length > 0){
        element = $$('div.category-products')[0];
        if (element.select('div.toolbar').length == 0){
            replace_toolbar = true;         
        }
    }else if($$('div.col-main p.note-msg').length > 0){
        element = $$('div.col-main p.note-msg')[0];
    }else{
        return;
    }

    if (content && content.toElement){
        content = content.toElement();
    }else if (!Object.isElement(content)) {

      content = Object.toHTML(content);
      var tempElement = document.createElement('div');
      content.evalScripts.bind(content).defer();
      content = content.stripScripts();
      tempElement.innerHTML = content;

      el =  this.getElementsByClassName('category-products', tempElement);

      if (el.length > 0){
         content = el[0];
      }else{
         el = this.getElementsByClassName('note-msg', tempElement);
         if (el.length > 0){
            content = el[0];
            if (this.gan_shop_by_area == 1){
                var shop_by_content = Object.toHTML(response.navigation);        
                shop_by_content.evalScripts.bind(shop_by_content).defer();
                shop_by_content = shop_by_content.stripScripts();                
                var tempElement = document.createElement('div');
                tempElement.innerHTML = shop_by_content;
                var shop_by = this.getElementsByClassName('block-layered-nav', tempElement);
                if (shop_by.length > 0)
                {
                    shop_by = shop_by[0];
                    shop_by.id = 'gan_tmp_shop_by';
                    new Insertion.Before(element, shop_by);
                }   
            }
         }else{
            return;
         }
      }
    }
    element.parentNode.replaceChild(content, element);

    if (replace_toolbar && $$('div.category-products').length > 0){
        this.ganReplaceToolbal(response.product_list);        
    }   

    if (typeof(need_scroll) != 'undefined' && need_scroll){
        if ($$('div.category-products').length > 0){
            var category_products = $$('div.category-products')[0];
            category_products.scrollTo();
        }
    }
},

After testing and failing again and again to solve this (used jquery live(), one(), livequery() and many many more..) I finally found a solution. 经过测试并一次又一次失败以解决此问题(使用了jquery live(),one(),livequery()等等),我终于找到了解决方案。

It's described in this article: Re-binding jQuery Events on AJAX Callbacks 本文对此进行了描述: 在AJAX回调上重新绑定jQuery事件

The code I used looks something like this: 我使用的代码如下所示:

function initBinding(){
    jQuery('.category-products-grid > .item').hover(function() {
        ...
        ...
    }, function() {
        ...
        ...
    });
}



var request = new Ajax.Request(url,
{
    ...
    ...
    onSuccess: function(transport){
        initBinding();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM