jQuery 将 click() 事件附加到 Facebook 的“喜欢”按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3718964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attach a click() event to the FaceBook 'Like' button?
提问by DaveDev
I'd like to do something on the page when a user clicks 'Like'.
当用户点击“喜欢”时,我想在页面上做一些事情。
I thought something simple like the following would work:
我认为像下面这样简单的事情会起作用:
<script type="text/javascript">
$(document).ready(function () {
$('.connect_widget_like_button clearfix like_button_no_like').live('click', function () {
alert('clicked');
});
});
</script>
This is the iFrame that FaceBook supplies to add the Like button to the page:
这是 FaceBook 提供的 iFrame,用于向页面添加“赞”按钮:
<iframe
id="FBLike"
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.MySite.Com... blahblahblah"
scrolling="no"
frameborder="0"
style="border:none; overflow:hidden; width:450px; height:80px;"
allowTransparency="true">
</iframe>
My script attempts to bind a click event to an anchor tag that is loaded into the iFrame when the page loads. It doesn't work though. Is there any way for me to get jQuery to recognise when this is clicked? Thanks
我的脚本尝试将单击事件绑定到页面加载时加载到 iFrame 中的锚标记。虽然它不起作用。有什么方法可以让 jQuery 识别出点击它的时间吗?谢谢
回答by serg
Facebook API doesn't allows you to subscribe to the like clicking event, but it allows you to subscribe to the like happening event (fired when the like action is completed):
Facebook API 不允许您订阅点赞点击事件,但它允许您订阅点赞发生事件(点赞操作完成时触发):
FB.Event.subscribe('edge.create', function(response) {
// like clicked
});
see here.
看到这里。
回答by Ben Everard
You cannot use JavaScript to access elements within an iframe that does not belong to the current URL, as I guess the domain you're using is not facebook.com you won't be able to attach an event to the like button in this way.
您不能使用 JavaScript 访问不属于当前 URL 的 iframe 中的元素,因为我猜您使用的域不是 facebook.com,您将无法以这种方式将事件附加到喜欢按钮.
回答by Damon Skelhorn
Make a PHP (or whatever language you use) script to CURL the source from http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.MySite.Com... blahblahblah
制作一个 PHP(或您使用的任何语言)脚本来 CURL 来源 http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.MySite.Com... blahblahblah
Put the source from facebook into your html page, you can then you can do whatever you like in JavaScript/jQuery with the code from Facebook.
将来自 facebook 的源代码放入您的 html 页面,然后您就可以使用来自 Facebook 的代码在 JavaScript/jQuery 中做任何您喜欢的事情。
Some examples for CURL within PHP - http://www.php.net/manual/en/curl.examples.php
PHP 中 CURL 的一些示例 - http://www.php.net/manual/en/curl.examples.php
回答by Joby Joseph
One can detect 'like' and 'unlike' event of facebook like button using edge.create
and edge.remove
respectively.
可以分别使用edge.create
和检测 facebook 喜欢按钮的“喜欢”和“不喜欢”事件edge.remove
。
回答by Miga Siilegmaa
<script type="text/javascript">
$(document).ready(function () {
$('.connect_widget_like_button clearfix like_button_no_like').live('click', function () {
alert('clicked');
});
});
</script>