在窗口加载时,我正在尝试检查我的图像是否已保存,如果已保存,请更改 HTML 以禁用保存它的链接。这种代码类型可以在没有.each()
with 的情况下工作,但只会找到第一个标签并根据它更改所有其他类似的标签。当然,我想分别做每个标签。通过查看 firebug 中的 post 事务,每个 ajax 调用都正确处理并发送了正确的href
值,但 html 更新似乎没有采取。if 语句没有失败,因为警报消息在它应该出现的时候出现。知道为什么$(this)
可以检索项目值但在设置 html 更改时不起作用?
window.onload = function() {
$(".img a").each(function() {
var item=$(this).attr( 'href' );
var action="check";
jqxhr = $.post("webservice.php", { action: action, color: item }, function(data) {
var result=data.result;
if (result=="saved") {
$(this).html("<div style='line-height:4em '>Saved</div>");
$(this).attr("href","saved");
alert(result);
}
}, "json")
.error(function() {
alert("error: unable to contact web service");
});
});
}