【问题标题】:Change all the user id to Facebook name将所有用户 ID 更改为 Facebook 名称
【发布时间】:2011-01-28 14:50:29
【问题描述】:

在我的页面中,有很多超链接。我想将所有带有 'uid' 属性的 'a' 更改为这样:

<a href=# uid="5526183">5526183</a> 

成为

<a href=# uid="5526183">Afrig Aminuddin</a>

另一个例子

<a href=# uid="5526183">My name is 5526183</a> 

成为

<a href=# uid="5526183">My name is Afrig Aminuddin</a>

首先:我会得到所有带有 'uid' 属性的 'a' 标签

var uid=$('a[uid]');

然后我调用该函数,该函数只会在页面加载后调用...

named(uid);

这是我的功能

function named(uid){

// here i'd like to do something with the 'uid' array in order it can be 
// processed in the next line

// in the next line, the 'uid' must be unique
// so, before that i'd like to filter all the 'uid' array in order to 
// filter the double 'uid'

 FB.api(
  {
    method: 'fql.query',
    query: 'SELECT name FROM user WHERE uid IN (5526183,1000014057,100001491471735)'
  },
  function(response) {
    // change all the 'a' with 'uid' attribute here...

    // response example
    return response[0].name; //return the name of 5526183
    return response[1].name; //return the name of 1000014057
    return response[2].name; //return the name of 100001491471735
  }
);}

【问题讨论】:

    标签: javascript jquery facebook


    【解决方案1】:
    $(document).ready(function () {
      var uid = $("a[uid]").eq(0).attr("uid");
    
      FB.api(
      {
          method: 'fql.query',
          query: 'SELECT name FROM user WHERE uid IN (' + uid + ')'
      },
      function(response) {
          $("a[uid]").each(function () {
              var $this = $(this);
    
              $this.text(response[0].name);
          });
      });
    });
    

    假设您每次都想获取它,这意味着您对每个链接都有不同的 uid,否则只需获取一次名称

    【讨论】:

    • @ivanov:该函数只会在页面加载后调用一次……如果每次都调用,完成时间会更长……
    • @ivanov,你能解释一下上面的代码是如何工作的吗,我是这种js代码的菜鸟。
    • 函数内部的 FB.api 调用是异步的,这意味着响应将在它被退休时触发,在这种情况下,您必须传递对链接对象的引用而不仅仅是 uid,这样您在实际数据检索时更新它
    • 好的,您将调用一次,我将对其进行编辑,使其按描述工作
    • 基本上,当文档准备好时,获取 uid,调用 FB,在响应更新所有链接时,假设您具有在 FB 上执行查询的正确权限并获得返回
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多