【问题标题】:Create custom Facebook share with Javascript使用 Javascript 创建自定义 Facebook 分享
【发布时间】:2015-01-31 06:13:39
【问题描述】:

我正在尝试使用 javascript 制作 facebook 分享按钮。

这是我的代码:

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: 'my app id',
        status: true,
        cookie: true,
        xfbml: true
    });
};
(function(d, debug) {
    var js, id = 'facebook-jssdk',
        ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {
        return;
    }
    js = d.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
    ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));

function postToFeed(title, desc, url, image) {
    var obj = {
        method: 'feed',
        link: url,
        picture: image,
        name: title,
        description: desc
    };

    function callback(response) {}
    FB.ui(obj, callback);
}

var fbShareBtn = document.querySelector('.fb_share'); //my problem start
fbShareBtn.addEventListener('click', function(e) {
    e.preventDefault();
    var title = fbShareBtn.getAttribute('data-title'),
        desc = fbShareBtn.getAttribute('data-desc'),
        url = fbShareBtn.getAttribute('href'),
        image = fbShareBtn.getAttribute('data-image');
    postToFeed(title, desc, url, image);

    return false;
});

这是 HTML:

<div id="fb-root"></div>        
<a href="mysite.com/mypost" data-image="my image" data-title="my title" data-desc="Let's See and comment" target="_blank" type="button" class="btnku btn-efbe fb_share" id="123">Facebook</a>

问题是分享按钮仅适用于帖子编号 1 或顶部帖子。我认为这是因为 javascript 没有赶上id

有答案吗?

【问题讨论】:

    标签: javascript php jquery facebook share


    【解决方案1】:

    您也可以从这里获取代码:https://www.addthis.com/dashboard#gallery/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      相关资源
      最近更新 更多