[英]Create custom Facebook share with Javascript
I'm trying to make facebook share button with javascript. 我正在尝试使用JavaScript使Facebook分享按钮。
Here is my code: 这是我的代码:
<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;
});
Here is the HTML: 这是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>
The problem is the share button only work for post number 1 or top post. 问题是共享按钮仅适用于帖子编号1或最高帖子。 I think it's because the javascript no catch the
id
我认为是因为javascript找不到
id
Any answer? 有答案吗?
您也可以从这里获取代码: https : //www.addthis.com/dashboard#gallery/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.