简体   繁体   English

jQuery-在加载所有元素后从另一页加载

[英]jQuery - load from another page after all elements are loaded

I want to display the rating of a product, witch is on the product page in div with id rating, on the category page, so I made a script below: 我想显示产品的等级,在类别页面上的id等级为div的产品页面上是巫婆,所以我在下面创建了一个脚本:

$('.product').each(function(){
    var url = $(this).find('a').attr('href');
    $(this).prepend('<div class="rating"></div>');
    $(this).find('.rating').load(url +'#rating');
});

The problem is, that the rating on the product page is generated with another script, so the element #rating is not present on the site from the start, so after doing some search I tried adding ajaxcomplete function: 问题是,产品页面上的评级是用另一个脚本生成的,因此从一开始网站上就没有元素#rating,因此在进行一些搜索后,我尝试添加ajaxcomplete函数:

$('.product').each(function(){
    var url = $(this).find('a').attr('href');
    $(this).prepend('<div class="rating"></div>');
    $(this).ajaxComplete(function(nxt) {
        $(this).find('.rating').load(url +'#rating');
        nxt();
    });
});

But that also doesn't seem to work, so I'm wondering is there any solution for this to work? 但这似乎也不起作用,所以我想知道是否有任何解决方案可以起作用?

Thanks 谢谢

You need to load the script that does the rating using jQuery getScript. 您需要使用jQuery getScript加载执行评级的脚本。 This way you can put processing dependent on the rating script in the success handler of the getScript call. 这样,您就可以将取决于评估脚本的处理置于getScript调用的成功处理程序中。

The code will look something like this: 该代码将如下所示:

$.getScript( "rating.js", function( data, textStatus, jqxhr ) {
  console.log( "rating complete" );
  $('.product').each(function(){
    var url = $(this).find('a').attr('href');
    $(this).find('.rating').load(url +'#rating');
  }
});

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

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