• 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 onclick="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);
       
    }

    效果如下

  • 相关阅读:
    按之字形数据打印二叉树
    对称的二叉树
    如何解决哈希冲突
    二叉树的下一节点
    删除链表中重复的节点
    链表的入环节点
    python入门
    js计算总页数
    将map中的值赋值给一个java对象
    给手机发送短信
  • 原文地址:https://www.cnblogs.com/aSnow/p/8824815.html
Copyright © 2020-2023  润新知