在 jQuery 中引用每个对象

发布于 2024-09-18 02:04:01 字数 2525 浏览 2 评论 0原文

问了一个关于在函数中引用对象的问题位于 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:[email protected]">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);
});

提前致谢,
何塞·莫雷拉

i had asked a question about refering objects in a function that is inside jquery each function but that first function is defined before in a array.

In my question everything went ok (because i was testing with one element only), when i put 2 elements, it didn't worked, it is calling the first always.

I think that is because ajax is assyncronous.

Can anyone give me a light?

The code is:

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:[email protected]">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);
});

Thanks in advance,
José Moreira

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2024-09-25 02:04:01

固定的,
config.connection 数组中添加了 async:false...

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:[email protected]">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);
});

谢谢大家...

Fixed,
added async:false at config.connection array...

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:[email protected]">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);
});

Thank you all...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文