jquery—使javascript在ajax响应后工作

a2mppw5e  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(321)

我有ajax显示帖子(无限滚动)。
我有一个脚本,它在ajax响应之前工作,但在响应之后不工作。
因此,当加载新帖子时,javascript不会在ajax加载的帖子中执行。

document.addEventListener('DOMContentLoaded', function() {
  jQuery(function($) {
    $('.post-link-home').find('.elementor-post').each(function() {
      let link = $(this).find('a').attr('href');
      $(this).wrapAll(document.createElement('a'));
      $(this).closest('a').attr('href', link);
    });
  });
});

在ajax加载帖子之后,有什么想法可以让它工作吗?

w41d8nur

w41d8nur1#

这就是我的工作原理:

document.addEventListener('DOMContentLoaded', function PostLinkHome() {
    jQuery(function($) {
        $('.post-link-home').find('.elementor-post').each(function() {
            let link = $(this).find('a').attr('href');
            $(this).wrapAll(document.createElement('a'));
            $(this).closest('a').attr('href', link);
        });
    });
    jQuery(document).ajaxComplete(function() {
        PostLinkHome();
    });
});

解释
我给了第一个不使用ajax执行的函数一个名称: PostLinkHome() 然后,我要求在加载ajax时执行相同的函数: jQuery(document).ajaxComplete(function() { PostLinkHome(); }); 来源

相关问题