使用 Facebook“FB.Canvas.getPageInfo”; jQuery 函数内部
我无法让 Facebook Javascript 函数在某些 jQuery 命令中工作...
这有效:
$("a#GetScrollTest").click( function() {
FB.Canvas.getPageInfo(function(fbCanvasInfoObject) {
var fbPosition = fbCanvasInfoObject.scrollTop;
$("a#GetScrollTest").html("The scroll top value
is " + fbPosition + "px");
});
return false;
});
这不起作用(返回“未捕获的引用错误:FB 未定义”):
$("a.scrollLink").each(function() {
$(this).click(function(){
FB.Canvas.getPageInfo(function(fbCanvasInfoObject) {
var fbPosition = fbCanvasInfoObject.scrollTop;
$(this).html("The scroll top value is " + fbPosition + "px");
});
return false;
});
});
任何人都知道为什么 click 方法有效,但 . each() 方法没有吗?为什么它返回FB未定义?呼叫都在同一个地方。
I can't get Facebook Javascript functions to work inside of certain jQuery commands...
This works:
$("a#GetScrollTest").click( function() {
FB.Canvas.getPageInfo(function(fbCanvasInfoObject) {
var fbPosition = fbCanvasInfoObject.scrollTop;
$("a#GetScrollTest").html("The scroll top value
is " + fbPosition + "px");
});
return false;
});
This doesn't (returns 'Uncaught ReferenceError: FB is not defined'):
$("a.scrollLink").each(function() {
$(this).click(function(){
FB.Canvas.getPageInfo(function(fbCanvasInfoObject) {
var fbPosition = fbCanvasInfoObject.scrollTop;
$(this).html("The scroll top value is " + fbPosition + "px");
});
return false;
});
});
Anyone know why one the click method works, yet the .each() method does not? Why does it return FB not defined? The calls are in the same place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以
window.fbAsyncInit
开头的大脚本块的全部要点是 Facebook SDK 异步加载。即使您在 jQuery 文档就绪回调中调用了
FB
,但这也不足以确保在执行该代码时加载 SDK。幸运的是,
window.fbAsyncInit
的存在正是为了这个目的:在 SDK 加载之前它不会运行。来自 Facebook 文档:
只需将您的
$("a.scrollLink").each(function()
移至 fbAsynchInit 函数中,一切都会很高兴。The whole point of that big script block starting with
window.fbAsyncInit
is that the Facebook SDK gets loaded asynchronously.Even though you've got your calls against
FB
inside a jQuery document ready callback, that isn't sufficient to ensure the SDK is loaded when that code is executed.Fortunately,
window.fbAsyncInit
exists for exactly that purpose: it won't be run until the SDK has loaded.From Facebook's docs:
Just move your
$("a.scrollLink").each(function()
into the fbAsynchInit function and all should be happy.