简体   繁体   English

添加它不加载ajax内容

[英]Addthis not loading on ajax content

I am trying to load addthis button on dynamically loaded content but even though the script is loaded addthis toolbar doesnt appear. 我试图在动态加载的内容上加载addthis按钮,但即使加载了脚本,也不会出现这个工具栏。

jQuery(".somediv").html(response); // dynamically loaded content
jQuery.getScript("//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-53a011f32e6cd338")
    .done(function () {
        addthis.init();
        addthis.toolbox('.addthis_sharing_toolbox');
    })

And below is the html content 以下是html内容

<div class="addthis_sharing_toolbox"></div>

Please help. 请帮忙。

@amit I was facing the same problem as you. @amit我遇到了和你一样的问题。 After some research, I rooted the source of this problem. 经过一番研究,我找到了这个问题的根源。

If you added your addthis button through the dashboard, there is some shortcut html/js codes to add them in your site very quickly: 如果您通过仪表板添加了addthis按钮,则会有一些快捷方式的html / js代码可以非常快速地将它们添加到您的站点中:

<!-- Go to www.addthis.com/dashboard to customize your tools -->
<div class="addthis_sharing_toolbox"></div>

But the formal way as the addthis api doc states to add the buttons to you site is like: 但正如addthis api doc声明要将按钮添加到您的网站的正式方式如下:

<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>

So, if you add the following lines inside your div box of class 'addthis_sharing_toolbox', it will finally work. 因此,如果您在“addthis_sharing_toolbox”类的div框中添加以下行,它将最终起作用。

<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>

Step-1: Put the following code into the main template from where you will call the ajax and also display the output: 步骤1:将以下代码放入主模板中,您将从该模板调用ajax并显示输出:

<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=**YOUR-PUB-ID**" async="async"></script>
//So that, we are inserting this AddThis JS source only for once;

Step-2: the dynamic (ajax) content should have the following addthis lines with other content: 第2步:动态(ajax)内容应该包含以下addthis行和其他内容:

<div class="addthis_sharing_toolbox">
    <a class="addthis_button_email" style="cursor:pointer"></a>
    <a class="addthis_button_facebook" style="cursor:pointer"></a>
    <a class="addthis_button_twitter" style="cursor:pointer"></a>
    <a class="addthis_button_linkedin" style="cursor:pointer"></a>
</div>

Step-3: Finally, after successful ajax load run the following line of code with the callback function; 步骤3:最后,在成功加载ajax后,使用回调函数运行以下代码行;

addthis.toolbox('.addthis_sharing_toolbox');

For example: 例如:

$.ajax({
    type: "GET",
    url: MY_URL,
    //......
    success: function(data){
        //......
        $("#MY-DIV").html(data); //THIS IS IMPORTANT TO INSERT THE DYNAMIC DATA INTO THE DOM BEFORE CALLING THE FOLLOWING TRIGGER;
        addthis.toolbox('.addthis_sharing_toolbox');
    }
});

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

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