<script src="./js/jquery-3.4.1.min.js"></script>
<script>
//根据class获取input file元素
let fileElements = $(".input-file").each(
(index,item)=>{
//对每个元素绑定change事件
$(item).change(
function(e)
{
//console.log(getObjectURL(this.files[0]));
找到用来预览的image并显示出来
$(this).parent().next().attr("src",getObjectURL(this.files[0]));
}
)
}
)
//获取文件绝对路径
function getObjectURL(file) {
var url = null;
if (window.createObjcectURL != undefined) {
url = window.createOjcectURL(file);
} else if (window.URL != undefined) {
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) {
url = window.webkitURL.createObjectURL(file);
}
return url;
}
</script>