更新 FB:Like URL 动态使用 JavaScript [英] Update FB:Like URL Dynamically using JavaScript

查看:25
本文介绍了更新 FB:Like URL 动态使用 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 javascript 动态地将 URL 更改为喜欢 FB:Like 按钮.

i'd like to change the URL to like of an FB:Like button dynamically using javascript.

现在我只能更改 fb:like 标签的 href 属性(我粘贴了下面的代码).但简单地更改 href 似乎不起作用.也许我必须重新启动 FB:Like 按钮,但到目前为止我不知道如何..

right now i've only been able to change the href attribute of the fb:like tag (i've pasted the code below). but simply changing the href doesn't seems to work. perhaps i have to re-initiate the FB:Like button, but so far i can't figure out how..

function update_share(container, url) {
  // update fb:like href value
  var container = container[0] || document.body;
  var button = container.getElementsByTagName('fb:like');
  button[0].setAttribute('href', url);
}

任何帮助将不胜感激.谢谢..

any help would be appreciated. thx..

推荐答案

它也适用于 XFBML 标签,无需使用 FB iframe,甚至适用于多个 FB,如 AJAX 调用

It works for me for XFBML tag as well without using FB iframe and even works with multiple FB like calls for AJAX

您只需要将整个 XFBML 代码包装在 JS/jQuery 中并如下所示进行解析:

You just need to wrap the entire XFBML code in JS/jQuery and parse it as shown below:

$('#like').html('<fb:like href="' + url + '" layout="button_count" show_faces="false" width="65" action="like" font="segoe ui" colorscheme="light" />');
if (typeof FB !== 'undefined') {
    FB.XFBML.parse(document.getElementById('like'));
}

HTML 代码:

<span id="like"></span>

这对更多人有帮助:)

这篇关于更新 FB:Like URL 动态使用 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆