添加这在ajax加载后无法正常工作

时间:2014-02-12 04:46:23

标签: javascript jquery ajax addthis

我有AddThis js用于将详细信息加入日历。这在页面加载时正常工作,但是我使用ajax加载进行了一些过滤并替换了html,之后AddThis按钮没有显示。这是我的代码ajax。

$('document').ready(function () {

    $('.eventSelect').change(function () {
       var selectedDate = $('#eventDate').val();
        var keyword = $('#eventsearch').val();
        var url = "/EventsHome?eventDate=" + selectedDate + "&keyword=" + keyword;
       $.ajax({
           type: "GET"
          , url: url
          , success: function (data) {
              console.log($(data).find(".eventList").html());
              $(".eventList").html($(data).find(".eventList").html());
             var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
                  if (window.addthis) {
                      window.addthis = null;
                      window._adr = null;
                      window._atc = null;
                      window._atd = null;
                      window._ate = null;
                      window._atr = null;
                      window._atw = null;
                  }
                  $.getScript(script);

             }
          , error: function (XMLHttpRequest, textStatus, errorThrown) {

          }, comeplete: 

10 个答案:

答案 0 :(得分:5)

加载新内容时使用此脚本:

if(typeof addthis !== 'undefined') { addthis.layers.refresh(); }

这只是 addthis_inline_share_toolbox 的解决方案!

addthis.toolbox()只有在addthis元素中指定了按钮时才有效(大部分是旧版本)。

答案 1 :(得分:4)

尝试自定义代码1小时后,我发现这个代码完美无缺。

$(document).ajaxStop(function() {
  if (window.addthis) {
    window.addthis = null;
    window._adr = null;
    window._atc = null;
    window._atd = null;
    window._ate = null;
    window._atr = null;
    window._atw = null;
  }
  return $.getScript("http://s7.addthis.com/js/300/addthis_widget.js#pubid=sdive");
});

这是source

答案 2 :(得分:3)

如果您只是致电:

,则无需重置AddThis变量并重新加载脚本
addthis.toolbox();

这应该根据您指定的任何配置重新呈现按钮。看一下这里的文档:

http://support.addthis.com/customer/portal/articles/1293805-using-addthis-asynchronously#.UvvWw0JdWTM

答案 3 :(得分:2)

本来会将此作为评论添加到Sol's answer,但我缺少代表。重新加载工具箱的实际语法是传入工具箱的类选择器,但这假设您已经初始化了addthis

 addthis.toolbox('.addthis_toolbox')

答案 4 :(得分:1)

您可以使用实时方法。

使用jQuery 1.4.2 $('。eventSelect')。live('change',function(){适用于FF,Safari,Opera, 但不适用于IE

答案 5 :(得分:1)

加里的回答为我做了: https://stackoverflow.com/a/21176470/3656694

然后跑步    addthis.toolbox('.addthis_toolbox')重新为我添加了addthis。

希望这有助于某人! :)

答案 6 :(得分:1)

为了在同一页面上使用ajax加载和/或多个addthis实例,您需要在div中插入元素:

<div class="addthis_toolbox" data-url="domain.com" data-title="title">
    <a class="addthis_button_facebook" style="cursor:pointer"></a> 
    <a class="addthis_button_twitter" style="cursor:pointer"></a> 
    <a class="addthis_button_email" style="cursor:pointer"></a>
</div>

然后在你的ajax完成之后运行addthis.toolbox('.addthis_toolbox')

答案 7 :(得分:1)

如果您正在使用AddThis共享按钮,您可能已经注意到,一旦在页面加载时初始化它,它就不适用于事后通过ajax加载的内容。

即。 AddThis不知道fresly加载的内容。对此的修复非常简单。只需按以下方式询问AddThis进行刷新,它将自动为新加载的内容重新生成正确的共享链接

addthis.layers.refresh();

答案 8 :(得分:1)

我意识到我的广告拦截器负责

if(typeof addthis !== 'undefined') { addthis.layers.refresh(); }

不工作。 (addthis.layers.refresh未定义。) 这很奇怪,因为我在4个站点上拥有完全相同的代码,但仅限于它不起作用的代码。因此,如果您遇到问题,请尝试关闭广告拦截器以查看是否有任何更改。

当我打开广告拦截器时,Alex发布的代码为我工作(uBlock起源)。我决定按如下方式运行它,这样我就可以实现&#34;对&#34;方式,只有当它不起作用,然后运行替代方式。

if (typeof addthis !== 'undefined') {
    if (typeof addthis.layers.refresh !== 'undefined' && $.isFunction(addthis.layers.refresh)) {
        addthis.layers.refresh();
    } else {
        if (window.addthis) {
            window.addthis = null;
            window._adr = null;
            window._atc = null;
            window._atd = null;
            window._ate = null;
            window._atr = null;
            window._atw = null;
        }
        return $.getScript("http://s7.addthis.com/js/300/YOURLINK");
    }
}

答案 9 :(得分:0)

我解决了这个问题! Link of Documentation

addthis_share.title = 'title of shared object';
addthis_share.url = 'URL to share';
addthis_share.description = 'description of shared';
addthis_share.swfurl ='URL of a Flash object to share, along with the link';
addthis_share.width	='ideal width of any provided Flash object';
addthis_share.height ='ideal height of any provide Flash object';
addthis_share.email_template ='name of template to use for emailed shares ';
addthis_share.email_vars ='associative array mapping custom email variables to values ';
<script type="text/javascript">
    var addthis_share = addthis_share||{
            url_transforms : {
                clean: true
            }
        };
</script>
<script type="text/javascript" src="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4e9c308d658c185a&async=1"></script>