在AJAX-Load之后,Facebook赞按钮刷新

Ale*_*exK 12 facebook facebook-social-plugins

我必须实现一个Facebook-Likebutton,其中url取自ajax请求...这意味着我在页面加载后设置了"href" - 属性.不幸的是,此时网址已经设置,所以当喜欢它时,它会将实际的页面网址作为网址...有没有办法刷新像按钮?

小智 43

如果您正在使用jQuery,那么这个问题就是一个非常简单明了的解决方案:

$(document).ajaxComplete(function(){
    try{
        FB.XFBML.parse(); 
    }catch(ex){}
});
Run Code Online (Sandbox Code Playgroud)


Jim*_*zuk 2

如果您使用 XFBML,我不知道如何更改 URL。但是,如果您使用 iframe,此代码片段应该可以工作:

超文本标记语言

<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.google.com&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;font&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true" id="theiframe"></iframe>
<br />
<input id="url" />
<input id="btn" value="Change" type="button">
Run Code Online (Sandbox Code Playgroud)

jQuery

$('#btn').click(function(){
    url = "";

    url += 'http://www.facebook.com/plugins/like.php?';
    url += 'href=' + $('#url').val() + '&amp;layout=standard&amp;';
    url += 'show_faces=true&amp;width=450&amp;action=like&amp;';
    url += 'font&amp;colorscheme=light&amp;height=80';

    $('#theiframe').attr({'src': url});
});
Run Code Online (Sandbox Code Playgroud)