//获取文件url function createObjectURL(blob){ if (window.URL){ return window.URL.createObjectURL(blob); } else if (window.webkitURL){ return window.webkitURL.createObjectURL(blob); } else { return null; } } function writeObj(obj){ var description = ""; for(var i in obj){ var property=obj[i]; description+=i+" = "+property+"\n"; } alert(description); } function uploads() { var filesizes=2097152; var box = $("#fileBox .review-box"); //显示图片box var file = $("#file"); //file对象 var domFragment = document.createDocumentFragment(); //文档流优化多次改动dom $("#fileBox").on("click", ".file-btn",function(){ var index = $(this).parent().index(); if(index == 6){ alert("最多可以上传4张图片!"); return false; } }); //触发选中文件事件 $("#fileBox").on("change", ".file-btn", function(event){ var imgNum = parseInt($("#fileBox .review-box img").length); if(imgNum < 4){ var file = event.target.files; //获取选中的文件对象 var imgTag = $(""); var fileName = file[0].name; //获取当前文件的文件名 var url = createObjectURL(file[0]); //获取当前文件对象的URL //忽略大小写 var jpg = (fileName.indexOf(".jpg") > -1) || (fileName.toLowerCase().indexOf(".jpg") > -1); var png = (fileName.indexOf(".png") > -1) || (fileName.toLowerCase().indexOf(".png") > -1); var jpeg = (fileName.indexOf(".jpeg") > -1) || (fileName.toLowerCase().indexOf(".jpeg") > -1); //判断文件是否是图片类型 if(jpg || png || jpeg||png){ imgTag.attr("src",url); }else{ alert("文件格式不支持!"); return false; } if(file[0].size>filesizes){ alert('图片不能大于2M'); return false; } //最佳显示 var imgbox = $("
×
"); imgbox.append(imgTag); box.append(imgbox); event.target.parentNode.style.display = "none"; var cloneDom = $(".clone-dom").eq(0).clone().removeAttr("style"); $("#fileBox").append(cloneDom); } }); // $(".review-box").on("click", ".prev-item", function(){ // var index = $(this).index(); // $(this).remove(); // $("#fileBox label:eq(" + (index + 1) + ")").remove(); // }); $(".review-box").on("click", ".closebtn", function(){ var this_f=$(this).parent(); var index = $(this_f).index(); $(this_f).remove(); $("#fileBox label:eq(" + (index + 1) + ")").remove(); }); } uploads();