【发布时间】:2023-03-18 17:27:02
【问题描述】:
我正在使用 javascript 来输出图像,在 FF 和 Chrome 中一切正常,但在 IE 中,只有在您第一次加载页面时才会附加第一张图像,只有在刷新页面后才会附加所有图像。
Javascript 输出图片:
$.getJSON("php/v.php?"+Math.random(),function(data){
$.each(data,function(l){
if(data[l]['s']==1){
$("#img"+(l+1)).append("<img src='"+data[l]['img']+"' />");
}
});
});
来自 v.php 的 IE9 开发工具响应:
[{"img":"https:\/\/www.example.com\/example.jpg","s":"1"},{"img":"https:\/\/www.example.com\/example.jpg","s":"1"},{"img":"https:\/\/www.example.com\/example.jpg","s":"1"}]
所以问题应该是由每个元素而不是getJSON引起的。 在 stackoverflow 上发现了许多类似的问题,但没有类似的问题 (http://stackoverflow.com/questions/7785138/each-method-only-applies-to-first-element-in-ie,http://stackoverflow.com/questions/4664190/javascript-each-loop-over -json-only-getting-first-element) 但在我的情况下,问题也出现在 IE9 中。
【问题讨论】:
-
你可以试着把你的 Javascript 放在 $(document).ready(function () { }
标签: javascript jquery json