html代码
其中number属性和id=img0的数组保持一致
<div align="center"> <input type="file" name="" class="file" number="0" multiple="multiple"><br> <img src="" id="img0" width="300px" class="img_file"> </div>
js代码
//上传图片 $(document).on('change',".file",function(){ var objUrl = getObjectURL(this.files[0]) ; console.log("objUrl = "+objUrl) ; var num=$(this).attr("number"); if (objUrl) { $("#img"+num).attr("src", objUrl) ; } }) ; //建立一個可存取到該file的url function getObjectURL(file) { var url = null ; if (window.createObjectURL!=undefined) { // basic url = window.createObjectURL(file) ; } else if (window.URL!=undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file) ; } else if (window.webkitURL!=undefined) { // webkit or chrome url = window.webkitURL.createObjectURL(file) ; } return url ; }