【发布时间】:2010-10-05 16:17:36
【问题描述】:
我有一个图像更新程序。我正在通过 JSON 加载显示我的图像的完全相同文件/部分。
我想写一个循环遍历一个索引并用它的相等替换每个图像。
这是我的 json 对象中的第一个 div:
[{
"photo": {
"position":1,
"photo_file_size":45465,
"created_at":"2010-10-05T09:51:13-04:00",
"updated_at":"2010-10-05T09:52:29-04:00",
"photo_file_name":"tumblr_l0x861Yc5M1qbxc42o1_400.jpg",
"is_cropped":true,
"crop_h":null,
"photo_content_type":"image/jpeg",
"id":216,
"caption":null,
"crop_w":null,
"photo_uploaded_at":null,
"crop_x":null,
"processing":false,
"gallery_id":26,
"crop_y":null
}
},
...
该 json 对象中的下一个 div 类似于 gallery_photos_attributes_1_id 。
更新
这是我到目前为止所得到的......但是 load() 方法不能正常工作。我得到一个“只允许请求”
$.getJSON(url, function(data) {
for (var i = 0; i < data.length; i ++) {
url2 = "/organizations/" + 1 + "/media/galleries/" + 26 + "/edit_photo_captions"
var image = $("#gallery_photos_attributes_" + i + "_caption");
url_str = image.siblings("img").attr("src").substr(0, str.lastIndexOf("/"));
image.siblings("img").load(url2, image.siblings("img"));
};
})
【问题讨论】:
-
如果您将“html”替换为 JSON 对象中的 id,会容易得多。有可能改变吗?
-
我们的想法是分离出隐藏在所有 HTML 中的一些内部细节,以便您的代码可以轻松找到它。因此,您可以按原样保留“html”属性,但 JSON 还应包含带有图像 ID 的“id”属性以及任何其他有用的数据。
-
你是对的。我让每张照片都成为 JSON 中的对象。现在我得到了未定义的 G 错误。
G is undefined [Break on this error] (function(){var l=this,g,y=l.jQuery,p=...ch(function(){o.dequeue(this,E)})}});
标签: javascript jquery json loops indexing