如何控制网址一旦分享按钮facebook点击

时间:2013-11-07 07:42:03

标签: php ajax facebook

我有一个for循环数据,其中每个都有一个facebook分享按钮,我的目标是当用户点击它将指向分配页面的分享按钮时。

继承人我的分享按钮div:

<img id = "share_button" src="img/share.png">

fb代码:

<script type="text/javascript">
 $(document).ready(function(){
   $('#share_button').click(function(e){
     e.preventDefault();
   FB.ui(
 {
    method: 'feed',
    name: 'This is the content of the "name" field.',
    link: ' http://www.test.com/',
    picture: '',
    caption: 'This is the content of the "caption" field.',
    description: 'This is the content of the "description" field, below the caption.',
    message: ''
    });
   });
 });
 </script>

有没有办法可以编辑ajax脚本:

 link: ' http://www.test.com/test.php?id="<?php echo $id; ?> ',

1 个答案:

答案 0 :(得分:0)

一种方法可以是将$ id存储为具有以下图像的属性:

<img id = "share_button" src="img/share.png" page_id=<?php echo $id?>/>

然后执行此操作:

<script type="text/javascript">
 $(document).ready(function(){
   $('#share_button').click(function(e){
     e.preventDefault();
     var id = $(this).attr('page_id');
   FB.ui(
 {
    method: 'feed',
    name: 'This is the content of the "name" field.',
    link: ' http://www.test.com/test.php?id='+ id,
    picture: '',
    caption: 'This is the content of the "caption" field.',
    description: 'This is the content of the "description" field, below the caption.',
    message: ''
    });
   });
 });
 </script>

请注意,如果您使用PHP生成HTML,这将有效。