简体   繁体   English

使用jQuery初始化时,Facebook般的按钮无法正常工作?

[英]Facebook like button won't work when initialised with jQuery?

I'm following this guide to load and initialise the facebook SDK. 我正在按照指南加载和初始化facebook SDK。 I checked console, and no errors were found. 我检查了控制台,没有发现错误。

$(document).ready(function(){
  $.ajaxSetup({ cache: true });
  $.getScript('//connect.facebook.net/en_US/all.js', function(){
    FB.init({
      appId: 'xxxx'
    });     
  });
});

Then I want to add a like button, as told here 然后我要添加一个喜欢的按钮,如此处所述

<div class="fb-like" data-send="true" data-width="450" data-show-faces="true"></div>

This fails to display anything on the my website, which is a bare canvas with <div id="fb-root"></div> being the only other element. 这无法在我的网站上显示任何内容,我的网站是一块裸露的画布,其中<div id="fb-root"></div>是唯一的其他元素。 However, if I initialise the JS SDK via pure javascript, the button works. 但是,如果我通过纯JavaScript初始化JS SDK,则该按钮有效。

Why won't it work when loaded up using the jQuery above? 使用上面的jQuery加载时,为什么它不起作用? Do I need to add something else? 我是否需要添加其他内容?

If you read the FB.init documentation, there are some parameters that can be used while defining FB.init . 如果您阅读FB.init文档,则在定义FB.init可以使用一些参数。 One of the parameter is xfbml which- 参数之一是xfbml -

Determines whether XFBML tags used by social plugins are parsed, and therefore whether the plugins are rendered or not. 确定是否解析社交插件使用的XFBML标签,并因此确定是否呈现插件。 Defaults to false. 默认为false。

You said that in case of pure javascript the like plugin worked that's beacause you must have set xfbml to true . 您说过,在使用纯JavaScript的情况下,类似的插件可以正常工作,因为您必须将xfbml设置为true To get your jquery code to work simply define this paramter to true which is false by default- 为了使您的jquery代码正常工作,只需将此参数定义为true,默认情况下为false-

$.getScript('//connect.facebook.net/en_US/all.js', function(){
    FB.init({
      appId: 'xxxx',
      xfbml: true
    });     

and it shall work. 它会工作。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM