如何让input(type="file")选中的图片显示在指定盒子里

https://img1.mukewang.com/5ca819a30001adeb03600161.jpg

泛舟湖上清波郎朗
浏览 433回答 1
1回答

qq_遁去的一_1

<input type="file" id="fileElem" multiple="" accept="image/*" onchange="handleFiles(this.files)"><div id="filegroup"></div>function handleFiles(files){&nbsp;&nbsp;&nbsp;var image-group= document.getElementById("filegroup");&nbsp; &nbsp; &nbsp; for (var i = 0; i < files.length; i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var img_box = document.createElement("div");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img_box.setAttribute("class","file-content");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var img = document.createElement("img");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img.src = window.URL.createObjectURL(files[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img.height = 100;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img.onload = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.URL.revokeObjectURL(this.src);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img_box.appendChild(img);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image-group.appendChild(img_box)&nbsp; &nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript