简体   繁体   English

Twitch API —获取名称

[英]Twitch API — Get Name

just wondering if anyone who's experienced using JQuery or twitch API could help with this. 只是想知道是否有经验使用JQuery或twitch API的人可以帮助您解决此问题。 Basically I am trying to get a username, but I do not want to have to click a button or display it in an input box. 基本上,我试图获取用户名,但是我不想单击按钮或将其显示在输入框中。

Here is the code from the API examples: 以下是API示例中的代码:

  $('#get-name button').click(function() {
    Twitch.api({method: 'user'}, function(error, user) {
      $('#get-name input').val(user.display_name);
    });
  })

So does anyone know how I would get the username without displaying it in an input box or have to click a button to display their username. 因此,有谁知道我如何获得用户名而不在输入框中显示用户名,或者必须单击按钮以显示其用户名。

You just need to make a call to Twitch.api as in the code example, but you don't need to worry about doing it inside of a click event handler. 您只需要像代码示例中那样调用Twitch.api ,但是您不必担心在click事件处理程序中执行此操作。

Twitch.api({method: 'user'}, function(error, user) {
  // this code runs after the Twitch API returns you the user data.
  // whatever you want to do with the username goes in here.
  // for example:
  console.log(user.display_name);
});

Let's say that somewhere in the HTML for your page, you have the following bit for displaying the username like you say: 假设您在页面HTML的某处,有如下显示用户名的位置:

<div class="greeting">
    Welcome, <span class="username"></span>
</div>

Then you can use 那你可以用

Twitch.api({method: 'user'}, function(error, user) {
  $('.greeting .username').text(user.display_name);
});

to fill the <span> with the user's name. 用用户名填充<span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM