我想发出$.get
请求,在响应时,修改特定索引处的DOM。例如,通过迭代地生成$ .get请求,用标题替换DOM中的所有ID,它看起来像:
$(".findme").each(function() {
// I want to replace ID inside findme with a title.
id = $(this).text();
index = $(this).attr("id");
$.get(url + "/" + id, function(data) {
// This doesn't work because $(this) is no longer the same
// as the one scoped to .findme
$(this).text(data.title);
});
$(this).removeClass("findme");
});
那样
<div class="findme" id="1">123432</div>
<div class="findme" id="2">432411</div>
可以成为
<div>Here's a fancy title</div>
<div>Here's a different title</div>
如何做到这一点?为方便起见,想象一下每个div都有如上所述的索引,也可以检索和使用。