0

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

1 回答 1

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,这将起作用。

于 2013-11-07T07:51:02.810 回答