

页面中多个,上传多个图片DEMO代码
<!Doctype html><html> <head><title>上传图片显示预览图</title><style> #result img{height:100px;display:inline-block;margin-right:10px;margin-bottom:10px; }</style> </head> <body><div class="add_imgs"> <p> <label>请选择一个图像文件:</label> <input type="file" id="file_input" style="display:none;" /></p><div id="result"><a href="javascript:void(0);" class="add_img_btn">添加图片</a> </div> </div><div class="add_imgs"> <p> <label>请选择一个图像文件:</label> <input type="file" id="file_input" style="display:none;" /></p><div id="result"><a href="javascript:void(0);" class="add_img_btn">添加图片</a> </div> </div><script src="jquery-2.2.1.min.js"></script><script> $(".add_img_btn").unbind("click").on("click",function(){$(this).parents(".add_imgs").find("input[type=file]").click();var result = $(this).parent(); var input = $(this).parents(".add_imgs").find("input[type=file]");dads(result,input); }) function dads(result,input){if(typeof FileReader==="undefined"){result.innerHTML = "抱歉,你的浏览器不支持 FileReader";input.setAttribute("disabled","disabled"); }else{$(input).unbind("change").on("change",function(){var file = this.files[0]; if(!/image/w+/.test(file.type)){alert("文件必须为图片!");return false; } var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function(e){$(result).append("<img src=""+this.result+"" alt="" />"); }})}}</script> </body></html>以上就是本文的全部内容,希望对大家学习JavaScript程序设计有所帮助。