我试图创建一个带有克隆并在循环中添加图像的函数,但它只添加一个,代码如下:
我的HTML:
<input type="file" name="files" id="files" multiple />
<ul id="img_public">
<li>
<img class="img_src" src="#" title="" alt="" />
<p class="img_name"></p>
<p class="img_size"></p>
</li>
</ul>
我的JS:
if($("#files").length > 0) {
var _URL = window.URL || window.webkitURL;
var clone = $('#img_public').children('li').clone();
$('#img_public').html('');
$("#files").change(function () {
var file, img;
$.each(this.files, function(i, elem) {
img = new Image();
img.onload = function(e) {
$('#img_public').append(clone);
clone.children('.img_src').attr('src',img.src).attr('alt',elem.name).attr('title',elem.name);
clone.children('.img_name').html(elem.name);
clone.children('.img_size').html(elem.size);
};
img.src = _URL.createObjectURL(elem);
});
});