我有这段代码
<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
为什么会这样?
答案 0 :(得分:0)
<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);
});
});
}