【问题标题】:Load jQuery script after Ajax (filter) loads加载 Ajax(过滤器)后加载 jQuery 脚本
【发布时间】:2016-08-12 11:00:02
【问题描述】:

我目前在同一页面上运行几个 jQuery 脚本,这个特定页面上有一个产品过滤器(使用 ajax)。 http://www.ctagroup.com.au/cta-group-home/products/selector/

我正在使用的一个脚本是这样的:

var $s = jQuery.noConflict();
$s('body').each(function () {
$s(this).html($s(this).html().replace(/(\®)/g, '<sup>®</sup>'));
});

因此,每个 ® 都会在站点范围内相应地使用。 单击过滤器选项后,它会在不运行我的脚本的情况下返回。

如何在每次使用 ajax 过滤器时再次运行此脚本?

【问题讨论】:

  • 放到一个函数中,从ajax过滤代码中调用...
  • 请注意,由于文档只有一个正文 $s('body').each(function () { $s(this).html($s(this).html().replace(/(\®)/g, '&lt;sup&gt;®&lt;/sup&gt;')); });$s('body').html($s('body').html().replace(/(\®)/g, '&lt;sup&gt;®&lt;/sup&gt;')); 相同,因此有效地替换整个正文内容 - 这样做可能会非常昂贵并且会破坏事情。
  • 仅供参考,在您的当前页面上,您在 var $s = jQuery.noConflict(); $s('body').each(function () { $s(this).html($s(this).html().replace(/(\®)/g, '&lt;sup&gt;®&lt;/sup&gt;')); }); 中收到无效的正则表达式标志(语法错误)看起来此代码可能位于您页面上的多个位置,并且全局无冲突 $s 可能是重复
  • 在页面上也将上述内容与重复的 sup 进行比较:var $s = jQuery.noConflict(); $s('body').each(function () { $s(this).html($s(this).html().replace(/(\&lt;sup&gt;®&lt;/sup&gt;)/g, '&lt;sup&gt;&lt;sup&gt;®&lt;/sup&gt;&lt;/sup&gt;')); }); 发生这种情况是因为您的全局替换,它会替换自身并爆炸 - 因此我在选择器。
  • 有一个双 的原因仅仅是因为我在整个网站的一些文本块周围都有 ,因为这是在我想出这个功能之前。其他没有 的 ® 现在都有一个。我用你的代码替换了我的代码,它所做的只是 div 类 .sf-result 中的 target ®。我认为你误解了我真正需要的东西。现在网站上的每个 ® 都有一个带有我的代码的 。使用过滤器时,您可以在产品描述中看到它恢复正常。 @马克

标签: javascript jquery ajax function filtering


【解决方案1】:

我不知道你是否需要jQuery.noConflict();,我不明白你为什么真的需要;但这是一个没有它的例子。注意我不喜欢你这样做的方式,使用你的页面我有点专注。您可以根据需要修改它,使用选择器定位特定元素。

在名为 customFixReg

的某处放置一个自定义事件处理程序
jQuery(document).on('customFixReg', function() {
  jQuery('.sf-result').find('.prod-cont-selector').find('p')
    .filter(':contains("®")').each(function() {
        jQuery(this).html(jQuery(this).html()
           .replace(/(\®)/g, '<sup>®</sup>'));
  });
});

编辑:不喜欢双选择器这也有效:

jQuery(document).on('customFixReg', function() {
  jQuery('.sf-result').find('.prod-cont-selector').find('p')
      .filter(':contains("®")').html(function(i, val) {
          return val.replace(/(\®)/g, '<sup>®</sup>');
      });
});

那么,什么时候;在你需要的地方,触发它:

jQuery(document).trigger('customFixReg');

我抓取了一些内容并在这里进行了测试:https://jsfiddle.net/MarkSchultheiss/j787Ljzz/

参考评论,您的页面在页面上的连续行中有这个:所以它根据之前的代码中断。页面第 789 行:

 <script>
var $s = jQuery.noConflict();
$s('body').each(function () {
$s(this).html($s(this).html().replace(/(\®)/g, '<sup>®</sup>'));
});
</script>

<script>
jQuery(document).on('customFixReg', function() {
  jQuery('ul.sf-result').find('.prod-cont-selector').find('h3,p')
    .filter(':contains("®")').each(function() {
        jQuery(this).html(jQuery(this).html()
           .replace(/(\®)/g, '<sup>®</sup>'));
  });
});
</script>

编辑:如果触发器重复出现,我发现了一个错误,它会自动换行,因此我将带有子 &lt;sup&gt; 元素的那些过滤掉。

jQuery(document).on('customFixReg', function() {
  jQuery('.sf-result').find('.prod-cont-selector').find('p,h3').filter(':contains("®")')
    .filter(function() {
      return !$(this).children('sup').length;
    })
    .each(function() {
      jQuery(this).html(function(i, val) {
        return val.replace(/(\®)/g, '<sup>®</sup>');
      });
    });
});

更新示例:https://jsfiddle.net/MarkSchultheiss/j787Ljzz/4/

【讨论】:

  • 我需要jQuery.noConfilct 的原因是因为我有其他脚本破坏了其他脚本。无论如何,我尝试使用在使用过滤器之前有效的事件处理程序。一旦使用过滤器,它就不起作用了。
  • 事件处理程序必须在未被替换的代码中,或者在页面刷新时发生这种情况时应该重新插入。
  • 我只能假设 .trigger 在链接库文件的某些代码中,对吧?
  • .trigger 位于事件处理程序的正下方
  • 很抱歉,我在页面上的任何地方都没有看到jQuery(document).trigger('customFixReg');(查看页面上的源代码)。该代码应放置在您的产品过滤器功能中(在过滤器和结果显示在页面上之后),该代码可能在您的 .js 文件之一中,但我对此一无所知。
猜你喜欢
  • 2016-05-22
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 2014-09-25
  • 2013-06-03
  • 1970-01-01
相关资源
最近更新 更多