js多图上传展示和删除

html部分

 <button class="btn btn-info" for="file">请选择文件</button>
<input type="file" id="file" name="file" multiple='multiple' class="hidden"  onchange="showPicture(this)"/>
<div id="imgBox" class="form-group">

js部分

var imgSrc = []; //图片路径
    var imgFile = []; //文件流
    var imgName = []; //图片名字
    var imgBox=$('#imgBox');
    function showPicture(imgF){
        var fileList=imgF.files;
        for(var i = 0; i < fileList.length; i++){
                    var imgSrcI = getObjectURL(fileList[i]);
                    imgName.push(fileList[i].name);
                    imgSrc.push(imgSrcI);
                    imgFile.push(fileList[i]);
            }
            addNewContent(imgBox);
  }
//图片展示
function addNewContent(obj) {
    $(imgBox).html("");
    for(var a = 0; a < imgSrc.length; a++) {
        //console.log(imgSrc);
        var oldBox = $(obj).html();
        $(obj).html(oldBox + '<div class="imgContainer col-sm-4"><img width="100%" class="img-rounded" title=' + imgName[a] + ' alt=' + imgName[a] + ' src=' + imgSrc[a] + ' ><span οnclick="sc('+a+')">删除</span></div>');
    }
}
//图片预览路径
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;
}
//删除
function sc(index){
    
   imgSrc.splice(index,1);
   index='';
   addNewContent(imgBox);
   
}

效果如下

转载于:https://www.cnblogs.com/aSnow/p/8824815.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值