2010-09-06 44 views
0

我有asked a question關於引用jQuery中每個函數內的函數中的對象,但第一個函數在數組中之前定義。在jQuery中引用對象每個

在我的問題一切正常(因爲我只測試一個元素),當我把2個元素,它沒有奏效,它是第一次總是調用。

我認爲這是因爲阿賈克斯是assyncronous。

任何人都可以給我一個燈?

的代碼是:

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); 
}); 

由於提前,
何塞·莫雷拉

+2

始終將您的代碼包含在您的實際問題中,而不是作爲鏈接。 StackOverflow是爲了站在自己的;其他資源可能會消失,改變路徑等等。您可以包含鏈接*以及*,但請確保代碼確實出現在您的問題中。 – 2010-09-06 16:05:32

+0

謝謝...修復,看我的問題 – CuSS 2010-09-08 09:53:26

回答

0

固定, 在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); 
}); 

謝謝大家.. 。