jQuery的/阿贾克斯 - $。阿贾克斯()传递参数回拨 - 良好的模式来使用? [英] jQuery / Ajax - $.ajax() Passing Parameters to Callback - Good Pattern to Use?

查看:198
本文介绍了jQuery的/阿贾克斯 - $。阿贾克斯()传递参数回拨 - 良好的模式来使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript的code我开始有:

JavaScript code I'm starting with:

function doSomething(url) {  
  $.ajax({ type: "GET",  
           url: url,  
           dataType: "xml",  
       success: rssToTarget  
       });  
}

模式我想用:

Pattern I would like to use:

//where elem is the target that should receive new items via DOM (appendChild)
function doSomething(url, elem) {
    $.ajax({ type: "GET",
         url: url,
         dataType: "xml",
     success: rssToTarget(elem)
       });
}

我不认为我能得到回调这样的工作方式,对不对?什么是正确的模式?我不想使用全局变量一定要临时保存ELEM或ELEM名称。

I don't think I can get the callback to work this way, right? What is the proper pattern? I don't want to use global variables necessarily to temporarily hold the elem or elem name.

推荐答案

就像这个...

function doSomething(url, elem) {
  $.ajax({
     type: "GET",
     url: url,
     dataType: "xml",
     success: function(xml) {
       rssToTarget(xml, elem);
     }
  });
}

回答您的评论:<一href="http://stackoverflow.com/questions/80802/does-use-of-anonymous-functions-affect-performance">Does匿名函数的使用会影响性能?

这篇关于jQuery的/阿贾克斯 - $。阿贾克斯()传递参数回拨 - 良好的模式来使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆