我試圖將圖像作爲輸入文件按鈕。選擇圖像後,默認圖像將變爲所選圖像。 通過使用fileReader
我只能夠定位一個圖像。使用fileReader讀取多個圖像
- 我怎樣才能使它只針對
image
attr在選定div
? - 這可能嗎?
HTML代碼
<div class="prep">
<div class="row">
<label>Step 1</label>
<img src="http://png-4.findicons.com/files/icons/129/soft_scraps/256/button_upload_01.png" id="upfile1" style="cursor:pointer" class="img"/ >
<input type="file" class="inputimg" />
</div>
</div>
<span class="add">Add Step</span>
JS代碼。
$(document).on("click" ,".img" , function(){
$(this).closest("div").find(".inputimg").trigger("click");
});
var count = 1;
$(".add").on("click",function(){
count ++;
var row = '<div class="row"><label>Step '+count+'</label><img src="http://png-4.findicons.com/files/icons/129/soft_scraps/256/button_upload_01.png" id="upfile1" style="cursor:pointer" class="img"/ ><input type="file" class="inputimg" />';
$(".prep").append(row);
});
$(".inputimg").change(function(){
readURL(this);
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.img').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
- 爲什麼動態添加的圖像,對齊不相同與第一個?
謝謝
在'label'標籤之後和'img'標籤之前添加空格可以解決問題。我已經更新了你的小提琴。 – rageit