我正在开发的网站包含通过 AJAX 动态加载的项目列表。当您向下滚动页面时,会加载更多项目。
现在,我的客户想要为每个项目添加一个类似 Facebook 的按钮(以及喜欢此按钮的人数)。
集成默认的点赞按钮没有问题,但是如何将点赞按钮添加到通过 AJAX 加载的项目中?如果加载一个新项目,Facebook API 如何知道 DOM 树中有一个新的点赞按钮,需要获取有多少人点赞该按钮?
有没有人有如何做到这一点的经验?有可用的例子吗?除了集成标准的按钮之外,我的搜索没有发现任何有用的东西。
The website I am working on consists of a list of items dynamically loaded via AJAX. As you scroll down the page, more items are loaded.
Now my customer wants to add a Facebook like button (and the number of people who liked this) to each of these items.
Integrating the default like button is no problem, but how do I add the like button to the items loaded via AJAX? If a new item is loaded, how does the Facebook API know that there is a new like button in the DOM tree for which it needs to fetch the count of how many people liked this?
Has anyone experience in how to do this? Is there an example available? My search didnt turn up anything usefull except integrating the standard like button.
发布评论
评论(4)
迟到的答案,但您可以在函数的回调中使用 Facebook API 的解析函数,该函数加载新元素以呈现新的类似按钮:
您还可以将解析器定向到您想要呈现的元素,因此它不会重新渲染已经存在的类似按钮:
这直接来自文档:https://developers .facebook.com/docs/reference/javascript/FB.XFBML.parse/
Late answer, but you could use the parse function of the Facebook API in a callback of your function that loads the new elements to render the new like buttons:
You can also direct the parser to the elements you want rendered, so it doesn't re-render the already existing like buttons:
This is straight from the docs: https://developers.facebook.com/docs/reference/javascript/FB.XFBML.parse/
迟到的答案,但以防万一有人需要它。
刚刚完成了我的项目,几乎与所描述的项目相同。
我的页面通过 ajax 获取 json 格式的帖子,然后我创建 dom 元素,包括 facebook like 按钮、tweet 按钮和 google plus one 按钮。在我解决问题之前,我遇到了很多问题。主要问题是,类似的按钮一次又一次无法按预期工作......之后我找到了有效的解决方案。
我正在使用 facebook js sdk (您可以找到一些有用的信息这里)
似乎ajax内容和FB.XFBML.parse() 方法或其他方法。
我在 fb 开发者论坛 上找到了解决方案,并对其进行了修改以适合我的情况需要。因此,在我从 ajax 调用获取 json 内容(多个帖子)后,我使用 jquery 创建除 fb 之类按钮之外的所有元素。我将帖子 url(以及其他一些相关数据)放入队列中,并使用超时调用函数来创建/解析这些按钮。
最后创建和解析 fb 像按钮
希望有人会发现这很有用,我知道一周前我会的:)
Late late answer but just in case someone need it.
Just finished my project nearly same as the one described.
My page gets posts via ajax in json format, and i than create dom elements including facebook like button, tweet button and google plus one button. I ve had lots of problems until i get it right. Major problem was that like buttons didnt work as expected again and again... After while i found working solution.
I am using facebook js sdk (you can find some useful info here)
It seems that there is a problem whit ajax content and FB.XFBML.parse() method or something.
I found a solution on fb developers forum, and modified it to fit my needs. So after i get json content (multiple posts) from ajax call, i create all elements with jquery except fb like buttons. I put the post url (and some other related data) in queue and call function with timeout to create/parse those buttons.
and finaly to create and parse fb like buttons
Hope someone will find this useful, I know that I would a week ago :)
Facebook 点赞按钮只是页面上的一个
。因此,没有运行“Facebook API”。点赞计数是
内部内容的一部分,当浏览器加载
src
URL 时,它会与其余内容一起加载。点赞按钮的代码如下所示:因此,本质上,您需要做的就是将新的
添加到页面的正确位置并使用正确的
src< /code> URL,其余所有内容将自动为您处理。
The Facebook like button is just an
<iframe>
on the page. There is no "Facebook API" running, as such. The like count is part of the content inside of the<iframe>
, and gets loaded with the rest of the content when thesrc
URL is loaded by the browser. The code for a like button goes like:So in essence, all you should need to do is add a new
<iframe>
to the page in the correct location and with the correctsrc
URL, and all the rest will be handled automatically for you.ajax 完成后调用此 javascript 函数;)
call this javascript function after ajax complete ;)