简体   繁体   English

AddThis 按钮在 AJAX 中不起作用,但可以正常工作

[英]AddThis button will not work inside AJAX, but will work normally

Basically, this is what I'm doing.基本上,这就是我正在做的。 User visits site, loads "index.html" Within index.html, it automatically loads, through AJAX, "details.html" into a DIV.用户访问站点,加载“index.html” 在 index.html 中,它会通过 AJAX 自动将“details.html”加载到 DIV 中。 I put an ADDTHIS button on "details.html".我在“details.html”上放了一个 ADDTHIS 按钮。 However, for some reason , the roll-over doesn't work.但是,由于某种原因,翻转不起作用。

When I visit details.html in the browser, the roll-over works.当我在浏览器中访问 details.html 时,翻转工作。 I'm guessing it's because of the AJAX?我猜这是因为 AJAX?

<a class="addthis_button"  href="http://www.addthis.com/bookmark.php?v=250&amp;pub=xa-4adf7e45288f5b21">
<img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="Bookmark and Share" style="border:0;margin-top:16px;"/></a>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pub=xa-4adf7e45288f5b21"></script>

I recently ran in to issues with using AddThis on an all AJAX site and was able to come up with a couple of solutions for this issue.我最近遇到了在所有 AJAX 站点上使用 AddThis 的问题,并且能够针对这个问题提出几个解决方案。

It turns out there is a variable that you can pass when loading the AJAX script as well as a way to re-initialize the script if the DOM is reloaded via AJAX.事实证明,您可以在加载 AJAX 脚本时传递一个变量,以及在通过 AJAX 重新加载 DOM 时重新初始化脚本的方法。 I have posted the full solution in detail on my blog here:我已经在我的博客上详细发布了完整的解决方案:

http://joecurlee.com/2010/01/21/how-to-use-addthis-with-ajax-and-overflowauto/ http://joecurlee.com/2010/01/21/how-to-use-addthis-with-ajax-and-overflowauto/

To briefly summarize, the solution is loading AddThis with the variable domready=1 appended, and re-initializing the script by deleting the initial load and then reloading the script dynamically:简单总结一下,解决方案是加载 AddThis 并附加变量 domready=1,并通过删除初始加载然后动态重新加载脚本来重新初始化脚本:

var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
if (window.addthis){
    window.addthis = null;
}
$.getScript( script );

addthis.toolbox(".addthis_toolbox");

if i understand your question correctly, in the callback of the ajax function, bind the roll-over to the add-this button.如果我正确理解你的问题,在 ajax 函数的回调中,将翻转绑定到 add-this 按钮。

 $.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(){
     $('.addthis_button').hover(
       function(){
         //do mouse over
       },function(){
         //do mouse out
     });
   }
 });

you can also try你也可以试试

$('.addthis_button').live('mouseover',function(){//do mouseover});
$('.addthis_button').live('mouseout',function(){//do mouseout});

i've never used live, but it seems like it would work for you since your add_this button's get created after the $(document).ready()我从来没有用过 live,但它似乎对你有用,因为你的 add_this 按钮是在 $(document).ready() 之后创建的

Ran in the same problem and this solved it for me in all major browsers (IE6+, FF, Safari in MAC/XP):遇到了同样的问题,这在所有主要浏览器(MAC/XP 中的 IE6+、FF、Safari)中为我解决了:

http://joecurlee.com/2010/01/21/how-to-use-addthis-with-ajax-and-overflowauto/comment-page-1/#comment-24 http://joecurlee.com/2010/01/21/how-to-use-addthis-with-ajax-and-overflowauto/comment-page-1/#comment-24

I had the same problem.我有同样的问题。 Fixed it with the following code.使用以下代码修复它。 I hope that fixes it for you too.我希望这也能为你解决。

Original Method:原始方法:

    var script = 'http://s7.addthis.com/js/300/addthis_widget.js?domready=1';
    if (window.addthis){
        window.addthis = null;
    }
    $.getScript( script );

New Method:新方法:

<script>
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);
</script>

In AJAX REQUEST you can use like this:-在 AJAX 请求中,您可以像这样使用:-

$.ajax({
    url:Url,
    type: 'POST',
    cache: false,
    success: function(res){
        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);
        // your custom code
    }
});

It seems like the script is calling the onLoad event of javascript and using this ajax call won't actually trigger that event.似乎脚本正在调用 javascript 的 onLoad 事件,并且使用此 ajax 调用实际上不会触发该事件。 You could try other "share this" widget?您可以尝试其他“共享此”小部件吗?

Like http://sharethis.com/#STS=g12m3ugh.21zb or pre-load that button?http://sharethis.com/#STS=g12m3ugh.21zb还是预加载那个按钮?

Can you post a little bit more of the story on what are you doing?你能多发表一些关于你在做什么的故事吗?

Is details.html a fully compliant page on it's own? details.html 本身是一个完全兼容的页面吗? HTML, BODY, HEAD tags, etc? HTML、BODY、HEAD 标签等?

If so, I think things can get kind screwy when you try to load it into another page.如果是这样,我认为当您尝试将其加载到另一个页面时,事情会变得有点棘手。 I would either change details.html to just include the barebones markup that you need - or - if you need it to still be individually accessible - you could use jQuery to strip out the needed bits after the ajax call and only inject that portion.我要么将 details.html 更改为仅包含您需要的准系统标记 - 或者 - 如果您需要它仍然可以单独访问 - 您可以使用 jQuery 在 ajax 调用后去除所需的位并仅注入该部分。

Details.html详情.html

<html>
<head>
</head>
<body>
    <div id="details">
    The needy bits.......
    </div>
</body>
</html>

Index.html索引.html

$("#targetDivID").load("detail.html #details");

Add this snippet of .js to the .html you are loading.将此 .js 片段添加到您正在加载的 .html 中。 Replace "#atbutton" with your button's selector.将“#atbutton”替换为按钮的选择器。

addthis.button("#atbutton"); addthis.button("#atbutton");

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

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