我目前正在同一页面上运行几个 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 过滤器时再次运行?

请您参考如下方法:

我不知道你是否需要这个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> 

编辑:我发现了一个错误,如果触发器重复出现,它会双重换行,所以我过滤了那些有 child 的 <sup>元素输出。

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/


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!