简体   繁体   English

如何确保Ajax使用.load完全加载内容

[英]How to make sure ajax is fully loading content using .load

We have a site where on the left we are displaying our products and on the right shows the product the user clicks on (on same page). 我们有一个网站,左侧显示我们的产品,右侧显示用户单击的产品(在同一页面上)。 When the page loads we have the first product being shown on the right by default, when the user clicks on a new product then the right columns changes to show the new product (via ajax). 当页面加载时,默认情况下,默认情况下,第一个产品显示在右侧,当用户单击一个新产品时,右列更改为显示新产品(通过ajax)。

Here is how I have the ajax setup: 这是我进行ajax设置的方法:

<script>
jQuery(document).ready(function($){
    var defaultValue = $("ul.products li.shop-thumb a:first").attr("href");

    $(".main").load(defaultValue);

  $("ul.products li.shop-thumb a").click(function(e){
      e.preventDefault();

      var addressValue = $(this).attr("href");
      //alert(addressValue );
    $(".main").load(addressValue);

    //$("a.woocommerce-main-image").addClass("image-popup-no-margins");

  });

});
</script>

We are using magnific popup to show the larger image of the product when the user clicks on the (right sides) main product image. 当用户单击(右侧)主产品图像时,我们使用放大的弹出窗口显示产品的较大图像。

This works great for the first (default) product but when the user clicks on a new product and the content to the right changes, then the user clicks on the main image, the pop up fails to load. 这对于第一个(默认)产品非常有用,但是当用户单击新产品并且右侧的内容发生更改,然后用户单击主图像时,将无法加载弹出窗口。 So in other words it works when you first load the page and the default product is shown, but fails to fire when a new product is clicked. 因此,换句话说,它在您首次加载页面并显示默认产品时有效,但是在单击新产品时无法启动。

Here is the product-image.php filter which just adds our class of image-popup-no-margins: 这是product-image.php过滤器,它仅添加了我们的image-popup-no-margins类:

apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image image-popup-no-margins" title="%s">%s</a>', $image_link, $image_title, $image ), $post->ID );

And in the footer we have this: 在页脚中有以下内容:

$('.image-popup-no-margins').magnificPopup({
      type: 'image',
      closeOnContentClick: true,
      closeBtnInside: false,
      fixedContentPos: true,
      mainClass: 'mfp-no-margins mfp-with-zoom', // class to remove default margin from left and right side
      image: {
        verticalFit: true
      },
      zoom: {
        enabled: true,
        duration: 300 // don't foget to change the duration also in CSS
      }
    });

Im may have given too much info here but I felt better safe then sorry, so perhaps its better if I give you a link for you to see what I mean. 我在这里可能提供了太多信息,但我觉得安全起着保护作用,但对不起,因此,如果我给您提供一个链接让您了解我的意思,那么可能会更好。 When you visit the page, click on the big image on the right and you should see the popup functioning. 当您访问该页面时,单击右侧的大图,您应该会看到弹出窗口的功能。 Now try clicking on one of the products on the left so the main image on right changes and try to click it again, the default behavior happens instead of our pop up. 现在,尝试单击左侧的产品之一,以便右侧的主图像发生更改,然后尝试再次单击它,将发生默认行为,而不是我们的弹出窗口。

I checked classes and everything looks fine, I'm sure it has to do with the ajax but can't get it worked out. 我检查了类,一切看起来都很好,我敢肯定它与ajax有关,但无法解决。

Update: After more tinkering with this I think the problem is that the .load is not 'reloading' the content when the anchor is clicked. 更新:对此进行了更多修改后,我认为问题是单击锚点时.load不会“重新加载”内容。 Like I said it works when you first load the page and click on the first image, but when you change the image and the ajax is called I don't think it really reloads the content in the main container. 就像我说的那样,它在您第一次加载页面并单击第一个图像时有效,但是当您更改图像并且调用了ajax时,我认为它并不会真正重新加载主容器中的内容。 How would I make sure the ajax is fully loading the content like it does when you first open the page? 我如何确保ajax像第一次打开页面一样完全加载内容? If you go to the site and click the big image on right, it works, change the image by clicking on a product from the left and the pop up fails to work and so does the "Tag It" pop up… Maybe this is less specific to magnific popup and more of a basic ajax 如果您访问该站点并单击右侧的大图像,它将起作用,通过单击左侧的产品来更改图像,弹出窗口将无法工作,因此“标记它”弹出窗口也可能会出现…也许是这样特定于壮观的弹出窗口以及更多基本的ajax

It seems your server is just quite slow and delivering the content required for the new popup. 看来您的服务器非常慢,无法提供新弹出窗口所需的内容。 you can see the get requests by viewing the firebug console. 你可以看到通过查看Firebug控制台的GET请求。 once these have completed it works perfectly. 一旦完成,它就会完美运行。 I suggest upgrading for hosting or finding another way to deliver the higher quality images faster, such as imgur or other image hosting services. 我建议升级托管服务或寻找另一种方法来更快地交付更高质量的图像,例如imgur或其他图像托管服务。

因此,问题确实出在宏大的弹出窗口上,用Fancybox代替了它,并且一切似乎都正常运行-感谢您的帮助

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

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