在AJAX无法完全运行后重新启动jQuery

时间:2021-09-07 03:53:57

I'm trying to add a jQuery gallery to a dynamic php page that uses an AJAX menu to filter products (Magento). When a selectable menu item is released or the 'clear all' button is pushed to reload the original stack of products, my gallery reloads and the script doesn't fully come back. The correct spaces for the gallery images come back like empty placeholders but the images do not show up. This is the 2 seperate scripts I tried to reinitialize the gallery:

我正在尝试将jQuery库添加到动态php页面,该页面使用AJAX菜单来过滤产品(Magento)。当一个可选择的菜单项被释放或者按下“全部清除”按钮以重新加载原始产品堆栈时,我的画廊重新加载并且脚本没有完全返回。图库图像的正确空间像空占位符一样返回,但图像不会显示。这是我尝试重新初始化图库的2个单独脚本:

Current script:

<script type="text/javascript" charset="utf-8">
    $(document).bind('m-ajax-after')(function() {
        $('#gallery').finalTilesGallery();
    });
</script>

1st attempt:

jQuery(document).bind('m-ajax-after', function(e, selectors, url, action) {
    $('#gallery').finalTilesGallery();
});

Before I added this script the gallery wouldn't display at all so I know I'm at least on the right track.

在我添加这个脚本之前,画廊根本不会显示,所以我知道我至少在正确的轨道上。

It looks like the extension that m-ajax-after is referring to is ManaDev SEO Layered Navigation Plus. http://www.manadev.com/seo-layered-navigation-plus/ I've found in multiple forums around the internet they suggest using this snippet to reinitialize any jQuery after their menu reloads:

看起来m-ajax-after所指的扩展名是ManaDev SEO Layered Navigation Plus。 http://www.manadev.com/seo-layered-navigation-plus/我发现在互联网上的多个论坛中,他们建议使用这个片段在菜单重新加载后重新初始化任何jQuery:

jQuery(document).bind('m-ajax-after', function (e, selectors, url, action) {
    // reinitialize your script 
});

They don't however offer any support as to how or why this works.

但是,它们不会提供任何支持,以确定其工作原理。

2 个解决方案

#1


Googling "m-ajax-after" doesn't really return much to work with. I can't find any documentation of this event, or what versions it applies to (or what it does).

谷歌搜索“m-ajax-after”并没有真正回归。我找不到此事件的任何文档,或者它适用于哪些版本(或它的作用)。

But, if you have such an event, you're attaching it incorrectly:

但是,如果你有这样的事件,你会错误地附加它:

$(document).bind('m-ajax-after') (function() {

The correct way to write it would be:

写它的正确方法是:

$(document).bind('m-ajax-after', function() {

Assuming that you haven't got an ancient version of jQuery though, there is a more prefered way:

假设你还没有古老版本的jQuery,那么有一种更优先的方式:

$(document).on('m-ajax-after', function() {

You can easily test if this gets called when you expect, by adding for example an alert:

您可以通过添加例如警报来轻松测试是否在您预期时调用此方法:

$(document).on('m-ajax-after', function() {
    alert('m-ajax-after invoked');
    $('#gallery').finalTilesGallery(); //Final Tiles Grid function apparently
}); 

Thing is though, that your 1st attempt is correctly written, and probably should have worked. But it's worth trying without all the parameters, and with the alert.

但是,你的第一次尝试是正确的,可能应该有效。但是没有所有参数和警报值得尝试。

#2


I fell into the same issue, Check you haven't got more than one jQuery coming into the page

我遇到了同样的问题,检查你没有多个jQuery进入页面

#1


Googling "m-ajax-after" doesn't really return much to work with. I can't find any documentation of this event, or what versions it applies to (or what it does).

谷歌搜索“m-ajax-after”并没有真正回归。我找不到此事件的任何文档,或者它适用于哪些版本(或它的作用)。

But, if you have such an event, you're attaching it incorrectly:

但是,如果你有这样的事件,你会错误地附加它:

$(document).bind('m-ajax-after') (function() {

The correct way to write it would be:

写它的正确方法是:

$(document).bind('m-ajax-after', function() {

Assuming that you haven't got an ancient version of jQuery though, there is a more prefered way:

假设你还没有古老版本的jQuery,那么有一种更优先的方式:

$(document).on('m-ajax-after', function() {

You can easily test if this gets called when you expect, by adding for example an alert:

您可以通过添加例如警报来轻松测试是否在您预期时调用此方法:

$(document).on('m-ajax-after', function() {
    alert('m-ajax-after invoked');
    $('#gallery').finalTilesGallery(); //Final Tiles Grid function apparently
}); 

Thing is though, that your 1st attempt is correctly written, and probably should have worked. But it's worth trying without all the parameters, and with the alert.

但是,你的第一次尝试是正确的,可能应该有效。但是没有所有参数和警报值得尝试。

#2


I fell into the same issue, Check you haven't got more than one jQuery coming into the page

我遇到了同样的问题,检查你没有多个jQuery进入页面