下面的代码在单击名为“button2”的按钮时成功触发FB.api()函数但是我无法从名为“button1”事件的按钮触发它,尽管我已经确认按钮正在触发它的点击处理程序。如果有人能告诉我我做错了什么,我会很感激。
window.fbAsyncInit = function() {
FB.init({ appId: 'xxxxxxxxxxxxxxx',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
function updateButton(response) {
var button1 = document.getElementById('subbtn');;
var button2 = document.getElementById('fb-auth');
button1.onclick = function(response){
alert("Button1 Clicked");
FB.api('/me', function(response) {
alert("FB.api Triggered");
});
};
if (response.authResponse) {
//user is already logged in and connected
var userInfo = document.getElementById('user-info');
FB.api('/me', function(response) {
userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name;
button2.innerHTML = 'Logout';
});
button2.onclick = function() {
FB.logout(function(response) {
var userInfo = document.getElementById('user-info');
userInfo.innerHTML="";
});
};
} else {
//user is not connected to your app or logged out
button2.innerHTML = 'Login';
button2.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
alert("FB.api triggered");
var userInfo = document.getElementById('user-info');
userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture" style="margin-right:5px"/>' + response.name;
});
} else {
//user cancelled login or did not grant authorization
}
}, {scope:'email'});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(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);
}());
答案 0 :(得分:0)
看起来两个onclicks之间的区别在于你在button2点击事件中调用FB.api之前调用FB.login,而不是在button1事件中调用:
button1.onclick = function(response){
alert("Button1 Clicked");
FB.api('/me', function(response) {
VS
button2.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
我猜你是否点击了button2然后再点击button1,那个button1的处理程序是否有效?