我正在尝试使用 jQuery 将 Like 按钮 和 Tweet 按钮 附加到页面。 Tweet 按钮工作正常,但 Like 按钮不会出现。我不知道出了什么问题,但更糟糕的是,我没有收到任何表明问题的错误或消息。
我怎样才能让 Facebook、Firebug 或任何东西告诉我缺少什么,或抛出适当的错误?
这是一个demonstration on jsfiddle .
这里是给那些不想去 jsfiddle 的人的完整代码:
/**
* Generate the container elements for social media stuff.
*/
$(function generateSocMedElements($) {
var id = "socmed",
$container,
$fb_root,
$fb_like,
$tweet_button;
if ($("#" + id).length <= 0) {
$container = $(document.createElement("aside")).
addClass("socmed").
attr("id", id);
$fb_root = $(document.createElement("div")).attr("id", "fb-root");
$fb_like = $(document.createElement("div")).
addClass("fb-like").
attr("data-href", "example.org").
attr("data-send", "false").
attr("data-layout", "button_count").
attr("data-width", 82).attr("data-show-faces", "false");
$tweet_button = $(document.createElement("a")).
addClass("twitter-share-button").
attr("href", "//twitter.com/share").
attr("data-count", "none").
text("Tweet");
$container.
appendTo($("header").first()).
append($fb_root).
append($fb_like).
append($tweet_button);
}
});
/**
* Load the script for the Facebook API.
*/
$(function loadFacebook($) {
var id = "facebook-jssdk";
if ($("#" + id).length <= 0) {
$(document.createElement("script")).
attr("id", id).
attr("src", "//connect.facebook.net/en_US/all.js").
appendTo("head");
}
});
/**
* Load the script for the Twitter API.
*/
$(function loadTwitter($) {
var id = 'twitterSdk';
if ($("#" + id).length <= 0) {
$(document.createElement("script")).
attr("id", id).
attr("src", "//platform.twitter.com/widgets.js").
appendTo("head");
}
});
最佳答案
http://jsfiddle.net/ShawnsSpace/btN89/3/我能够让它工作
- 在header中添加div标签
- 将 generateSocMedElements() 包装在函数中并在页面加载后调用。
- 使用 facebooks html5 脚本。
.您将不得不调整它以获得所需的外观。
<header><h1>OH HAI I HAZ A HEADER</h1>
<div id="fb-root"></div>
</header>
/**
* Load the script for the Facebook API.
*/
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
/**
* Load the script for the Twitter API.
*/
$(function loadTwitter($) {
var id = 'twitterSdk';
if ($("#" + id).length <= 0) {
$(document.createElement("script")).
attr("id", id).
attr("src", "//platform.twitter.com/widgets.js").
appendTo("head");
}
});
function loadthis($){
$(function generateSocMedElements($) {
var id = "socmed",
$container,
$fb_root,
$fb_like,
$tweet_button;
if ($("#" + id).length <= 0) {
$container = $(document.createElement("aside")).
addClass("socmed").
attr("id", id);
$fb_root = $(document.createElement("div")).attr("id", "fb-root");
$fb_like = $(document.createElement("div")).
addClass("fb-like").
attr("data-href", "example.org").
attr("data-send", "false").
attr("data-layout", "button_count").
attr("data-width", 82).attr("data-show-faces", "false");
$tweet_button = $(document.createElement("a")).
addClass("twitter-share-button").
attr("href", "//twitter.com/share").
attr("data-count", "none").
text("Tweet");
$container.
appendTo($("header").first()).
append($fb_root).
append($fb_like).
append($tweet_button);
}
});
}
loadthis($);
关于javascript - 如何调试缺少的 Facebook Like 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7931440/