Javascript 为什么我们需要添加 <div id="fb-root"></div>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8097121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why we need to add <div id="fb-root"></div>
提问by Tinggo
Why we need to add these pair of tags within our facebook application. What's the use of this pairs of tags. I created an application which use an apprequest, and it works good even if I didn't add these tags in front of my scripts. So I really wonder why we need to add them. Thank you.
为什么我们需要在我们的 facebook 应用程序中添加这对标签。这对标签有什么用。我创建了一个使用 apprequest 的应用程序,即使我没有在脚本前添加这些标签,它也能很好地工作。所以我真的想知道为什么我们需要添加它们。谢谢你。
采纳答案by Simon Hutton
It's the place holder for the Facebook javascript script to attach elements to the DOM. Without this when the referenced Facebook script is ran it has nowhere to attach elements.
它是 Facebook javascript 脚本的占位符,用于将元素附加到 DOM。如果没有这个,当引用的 Facebook 脚本运行时,它就无法附加元素。
You can see fb-root gets appended to as part of the initialisation.
您可以看到 fb-root 作为初始化的一部分附加到。
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({appId: 'xxxxxx', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
回答by Doug S
UPDATE: Facebook no longer requires that you include <div id="fb-root"></div>
in your HTML.
更新:Facebook 不再要求您包含<div id="fb-root"></div>
在您的 HTML 中。
You can now remove it. The Facebook Javascript SDK creates it on its own, appending it to the BODY
tag. No warnings are shown in the console either, as it did before.
您现在可以删除它。Facebook Javascript SDK 自行创建它,并将其附加到BODY
标签中。控制台中也不显示警告,就像以前一样。
The Facebook documentation has also been updated, no longer showing the <div id="fb-root"></div>
requirement.
Facebook 文档也已更新,不再显示<div id="fb-root"></div>
要求。
Old documentation for version 1.0 (shows <div id="fb-root"></div>
):
https://developers.facebook.com/docs/javascript/quickstart/v1.0
1.0 版的旧文档(显示<div id="fb-root"></div>
):https:
//developers.facebook.com/docs/javascript/quickstart/v1.0
Current documentation for version 2.5 (no longer shows <div id="fb-root"></div>
): https://developers.facebook.com/docs/javascript/quickstart/v2.5
2.5 版的当前文档(不再显示<div id="fb-root"></div>
):https: //developers.facebook.com/docs/javascript/quickstart/v2.5
回答by Pierre Olivier Martel
It gets even more simple with Turbolinks 5. You just need to add data-turbolinks-permanent=true
to the fb-root
div and it will be persisted between requests. No need to fiddle with the javascript.
有了 Turbolinks 5,它变得更加简单。您只需要添加data-turbolinks-permanent=true
到fb-root
div 中,它就会在请求之间持久化。无需摆弄javascript。