在 jQuery 中引用每个对象
我问了一个关于在函数中引用对象的问题位于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
固定的,
在
config.connection
数组中添加了async:false
...谢谢大家...
Fixed,
added
async:false
atconfig.connection
array...Thank you all...