我有这个密码
<span
id="signinButton"
class="g-signin"
data-callback="signinCallback"
data-clientid="383980626115-6g1kc77ltl2edrh4dopdmfasu0r7j7dn.apps.googleusercontent.com"
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-scope="https://www.googleapis.com/auth/plus.profile.emails.read"
data-width="wide"
data-height="standard"
style="display:none;"
>
</span>
我想在登录时删除此按钮,因此在登录时执行此操作:-
if (AuthStates.google['access_token']) {
// Signed in with Google, you can now use Google+ APIs.
console.log(AuthStates.google);
document.getElementById('signinButton').setAttribute('style', 'display: none');
gapi.client.load('plus','v1', function(){
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
document.getElementById('cname').value =resp.displayName;
document.getElementById('cemail').value =resp.emails[0].value;
console.log('Retrieved profile for:' + resp.displayName + ' ' + resp.emails[0].value);
});
});
}
但它会抛出错误:
Uncaught TypeError: Cannot read property 'setAttribute' of null google+ signin
为什么会这样?
最佳答案
<span
id="signinButton"
class="g-signin"
data-callback="signinCallback"
data-clientid="383980626115-6g1kc77ltl2edrh4dopdmfasu0r7j7dn.apps.googleusercontent.com"
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-scope="https://www.googleapis.com/auth/plus.profile.emails.read"
data-width="wide"
data-height="standard"
</span>
对于样式编辑,不应使用setAttribute
if (AuthStates.google['access_token']) {
// Signed in with Google, you can now use Google+ APIs.
console.log(AuthStates.google);
document.getElementById('signinButton').style.display= 'none';
gapi.client.load('plus','v1', function(){
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
document.getElementById('cname').value =resp.displayName;
document.getElementById('cemail').value =resp.emails[0].value;
console.log('Retrieved profile for:' + resp.displayName + ' ' + resp.emails[0].value);
});
});
}
关于javascript - 登录时Google+ span元素消失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24103841/