在jQuery中引用对象

时间:2010-09-06 16:02:50

标签: jquery ajax function each

我有asked a question关于在jquery里面的函数中引用每个函数的对象,但第一个函数是在数组之前定义的。

在我的问题中,一切顺利(因为我只测试了一个元素),当我放入2个元素时,它没有用,它总是调用第一个。

我认为这是因为ajax是同步的。

任何人都可以给我一个亮点吗?

代码是:

var $d=null;
var configs={
  general:{
    selector:'div.MicrodualAdGet',
    max_ads:6},
  logs:{
    selector:'div#MicrodualAdGet-debug'},
  connection:{
    type:'POST',
    url:'http://www.microdual.com/api/microdualgetad',
    cache:false,
    timeout:20000,
    dataType:'json',
    data:{
      adget_id:null,
      client_action:null},
    error:function(r,s,e){MicrodualAdGet_Log("Ajax-Error: "+s+"\n");},
    success: null
  }
};
function MicrodualAdGet_Log(msg){$(configs.logs.selector).append(msg);}
function MicrodualAdGet_View(d,s){
  if(! d) {
    MicrodualAdGet_Log("MicrodualAdGet-Error: Couldn't contact server correctly\n");
    $d.replaceWith("MicrodualAdGet Error: Couldn't contact server correctly");
  }else{
    if(d.hackattemp.status){
      MicrodualAdGet_Log("MicrodualAdGet-Hackattemp-Error: "+d.hackattemp.id+"\n");
      $d.replaceWith("MicrodualAdGet Hackattemp Error: "+d.hackattemp.id);
    }else{
      var content='';
      $d.css({
        display: 'block',
        position: 'relative',
        width: d.tamanhos[d.adpost.id_tamanho].width,
        height: d.tamanhos[d.adpost.id_tamanho].height,
        overflow: 'hidden',
        top: '0px',
        left: '0px',
        background: '#000000'
      })
      if(d.tipos[d.adpost.id_tipo].nome_tipo=='text') content=d.adpost.content;
      if(d.tipos[d.adpost.id_tipo].nome_tipo=='image') content='<a href="'+d.adpost.link+'" target="_blank"><img src="'+d.adpost.content+'" width="'+d.tamanhos[d.adpost.id_tamanho].width+'" height="'+d.tamanhos[d.adpost.id_tamanho].height+'" border="0" /></a>';
      if(content=='') content='MicrodualAdGet Error: Unable to determine ad type. Please contact our <a href="mailto:network@microdual.com">Network Administrator</a>';
      $d.replaceWith(content);
    }
  }
}



configs.connection.success = MicrodualAdGet_View;
configs.connection.data.client_action = "view";
$(configs.general.selector).each(function(){
  $d=$(this);
  configs.connection.data.adget_id=$(this).attr("rel");
  $.ajax(configs.connection);
});

提前致谢,
何塞莫雷拉

1 个答案:

答案 0 :(得分:0)

固定, 在async:false数组添加config.connection ...

var $d=null;
var configs={
  general:{
    selector:'div.MicrodualAdGet',
    max_ads:6},
  logs:{
    selector:'div#MicrodualAdGet-debug'},
  connection:{
    type:'POST',
    url:'http://www.microdual.com/api/microdualgetad',
    cache:false,
    async:false,
    timeout:20000,
    dataType:'json',
    data:{
      adget_id:null,
      client_action:null},
    error:function(r,s,e){MicrodualAdGet_Log("Ajax-Error: "+s+"\n");},
    success: null
  }
};
function MicrodualAdGet_Log(msg){$(configs.logs.selector).append(msg);}
function MicrodualAdGet_View(d,s){
  if(! d) {
    MicrodualAdGet_Log("MicrodualAdGet-Error: Couldn't contact server correctly\n");
    $d.replaceWith("MicrodualAdGet Error: Couldn't contact server correctly");
  }else{
    if(d.hackattemp.status){
      MicrodualAdGet_Log("MicrodualAdGet-Hackattemp-Error: "+d.hackattemp.id+"\n");
      $d.replaceWith("MicrodualAdGet Hackattemp Error: "+d.hackattemp.id);
    }else{
      var content='';
      $d.css({
        display: 'block',
        position: 'relative',
        width: d.tamanhos[d.adpost.id_tamanho].width,
        height: d.tamanhos[d.adpost.id_tamanho].height,
        overflow: 'hidden',
        top: '0px',
        left: '0px',
        background: '#000000'
      })
      if(d.tipos[d.adpost.id_tipo].nome_tipo=='text') content=d.adpost.content;
      if(d.tipos[d.adpost.id_tipo].nome_tipo=='image') content='<a href="'+d.adpost.link+'" target="_blank"><img src="'+d.adpost.content+'" width="'+d.tamanhos[d.adpost.id_tamanho].width+'" height="'+d.tamanhos[d.adpost.id_tamanho].height+'" border="0" /></a>';
      if(content=='') content='MicrodualAdGet Error: Unable to determine ad type. Please contact our <a href="mailto:network@microdual.com">Network Administrator</a>';
      $d.replaceWith(content);
    }
  }
}



configs.connection.success = MicrodualAdGet_View;
configs.connection.data.client_action = "view";
$(configs.general.selector).each(function(){
  $d=$(this);
  configs.connection.data.adget_id=$(this).attr("rel");
  $.ajax(configs.connection);
});

谢谢大家...