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


How to control url once share button facebook clicks

我得到了一个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; ?> ',

一种方法是将$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>

请注意,如果您正在生成 HTML usinh PHP,这将起作用。