javascript - Google+ span 元素在登录时消失

标签 javascript html google-plus

我有这个代码

<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/

相关文章:

html - 为什么向 div 添加新元素会插入整个 div 向上?

python - 使用 python 客户端使用 Google Plus API 获取用户的圈子列表

flash - 在 Google Plus 的流中共享自定义视频播放器

javascript - 尽管经过验证,Google HTML 表单仍会提交

javascript - angular-ui-tree 拖动不起作用

javascript - 如何使用 Javascript 从伪元素获取 FontAwesome 字符代码

javascript - 从 AngularJS 中的选择框中选择选项后专注于输入文本

html - 表格单元格被推到下一行

Android-Sign in via Google plus "An internal error has occured"

javascript - 当ImageData不可用时如何提供正确的数据?