2011-10-28 17 views
3

我试图附加像按钮Tweet按钮到使用jQuery的页面。 Tweet按钮工作得很好,但不会出现Like按钮。我不知道有什么问题,但更糟糕的是,我没有收到任何指出问题的错误或消息。如何调试缺少的Facebook 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"); 
    } 
}); 
+0

究竟什么是你想用这个代码来完成?据我所知,你正试图动态地加载按钮......但你使用“#”和“id”来选择元素,但也使用“addClass”。您是否试图在页面上找到“Like”和“Tweet”链接,并用动态生成的按钮替换它们? –

+0

在js小提琴看起来像all.js没有被追加出于某种原因。我会继续玩这个游戏,并发布任何结果。我建议现在只使用iframe的iframe版本。 –

+0

@CalebGray当这个脚本运行时,Like和Tweet按钮不应该存在,但是我为了防止它而投入了一个检查。应该发生的情况是,Tweet和Like按钮应该作为附加到页面上第一个“header”元素的'aside'元素的子元素出现。 – kojiro

回答

4

http://jsfiddle.net/ShawnsSpace/btN89/3/我能得到它的

  1. 工作中添加div标签头文件
  2. 在函数中包装generateSocMedElements()并在页面加载后调用。
  3. 使用了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($);