首页 > 编程语言 >自定义上传图片,动态拼接html元素,node插入/替换指定位置旧元素

自定义上传图片,动态拼接html元素,node插入/替换指定位置旧元素

时间:2023-06-07 09:11:05浏览次数:53  
标签:node flex 自定义 image 元素 height width margin display

<!DOCTYPE html>
<head>     <meta name="viewport"         content="width=device-width,initial-scale=0.5,maximum-scale=1.0,minimum-scale=0.5,user-scalable=yes">     <title>上传图片</title> </head>
<body>     <div class="info-title">         <span>上传图片</span>     </div>     <div class="div-image">         <div id="imageContent" class="div-image-upload">         </div>         <input class="uploadImg" type="file" name="file1" id="filePath" accept="image/*">         <br>         <div class="reg"><a href="/info">迁移</a></div>         <br>     </div> </body> <script>     const inputFile = document.getElementById("filePath");     const imageContent = document.getElementById("imageContent");     var list = [];     var uploadIndex = 0;     inputFile.addEventListener('change', function (event) {         const { files } = this;         const f = files[0];         const imageSrc = URL.createObjectURL(f);         const template = "<div class='div-image-item'>" +             "<img src=" + imageSrc + " alt='' id='image" + uploadIndex + "'>" +             "<div class='image-close' onclick='delImageClick(" + uploadIndex + ")'><span></span> </div>" +             "</div>";         let tempNode = document.createElement('div');         tempNode.innerHTML = template;         // 替换指定的节点         imageContent.replaceChild(tempNode.firstChild, imageContent.childNodes[uploadIndex]);         list[uploadIndex] = imageSrc;
        // 允许相同文件上传         event.target.value = "";     });     window.onload = function () {         // TODO 数据获取         list = ["https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png",             "https://img.alicdn.com/tfs/TB1Z0PywTtYBeNjy1XdXXXXyVXa-186-200.svg", "", "", "", "", "", "", ""];
        showData();     };     // 显示数据     function showData() {         var htmlStr = "";         for (var i = 0; i < list.length; i++) {             var item = list[i];             if (item === "") {                 htmlStr += "<div id='btnUpload" + i + "' class='div-image-item2 btn-upload'>" +                     "<div class='upload-image' onclick='uploadImage(" + i + ")'>" +                     "<div class='add'></div>" +                     "</div>" +                     "</div>";             } else {                 htmlStr += "<div class='div-image-item'>" +                     "<img src=" + item + " alt='' id='image" + i + "'>" +                     "<div class='image-close' onclick='delImageClick(" + i + ")' ><span></span> </div>" +                     "</div>";             }         }         imageContent.innerHTML = htmlStr     }     // 触发上传文件     function uploadImage(currIndex) {         uploadIndex = currIndex;         inputFile.click();     }     // 删除图片     function delImageClick(currIndex) {         // list.splice(currIndex, 1);         list[currIndex] = "";         const template = "<div id='btnUpload" + currIndex + "' class='div-image-item2 btn-upload'>" +             "<div class='upload-image' onclick='uploadImage(" + currIndex + ")'>" +             "<div class='add'></div>" +             "</div>" +             "</div>";         let tempNode = document.createElement('div');         tempNode.innerHTML = template;         // 替换指定的节点         imageContent.replaceChild(tempNode.firstChild, imageContent.childNodes[currIndex]);     }; </script> <style>     body {         padding: 0;         margin: 0;         height: 100vh;         background-color: #9e9e9e30;         height: 100vh;     }

    .info-title {         margin: auto;         top: 5%;         position: relative;
        text-align: center;     }
    .info-title span {         color: #3c3c3c;         font-size: 24px;         font-weight: bold;     }
    .div-image {         display: flex;         align-items: center;         margin: auto;         flex-direction: column;         top: 10vh;         position: relative;         flex-wrap: wrap;         width: 80%;     }
    .div-image-upload {         display: flex;         justify-content: flex-start;         flex-wrap: wrap;         width: 100%;     }
    .div-image-item {         flex: 0 0 calc(33.33% - 4rem - 4px);         margin: 2rem;         position: relative;
        color: #ccc;         border: 2px dashed;     }
    .div-image-item2 {         flex: 0 0 calc(33.33% - 4rem);         margin: 2rem;         position: relative;     }
    .btn-upload {         cursor: pointer;     }
    .disabled-upload {         pointer-events: none;         cursor: not-allowed;     }
    .disabled-upload .add {         background: #dddddd;     }
    .add {         display: inline-block;         width: 100%;         height: 15rem;         color: #ccc;         border: 2px dashed;         transition: color .25s;         position: relative;         overflow: hidden;     }
    .add:hover {         color: #34538b;     }
    .add::before,     .add::after {         content: '';         position: absolute;         top: 50%;         left: 50%;     }
    .add::before {         width: 20px;         border-top: 4px solid;         margin: -2px 0 0 -10px;     }
    .add::after {         height: 20px;         border-left: 4px solid;         margin: -10px 0 0 -2px;     }
    img {         object-fit: contain;         width: 100%;
        height: 15rem;         margin: auto;     }
    input.uploadImg {         display: inline-block;         width: 100%;         height: 100%;         display: none;     }
    .image-close {         width: 40px;         height: 40px;         border: 1px solid #F44336;         border-radius: 50%;         line-height: 35px;         text-align: center;         position: absolute;         right: -10px;         top: -10px;         cursor: pointer;     }
    .image-close:hover {         border: 1px solid #f11404;     }
    .image-close span {         display: inline-block;         width: 30px;         height: 4px;         background: #F44336;         transform: rotate(45deg);     }
    .image-close span::after {         content: '';         display: block;         width: 30px;         height: 4px;         background: #F44336;         transform: rotate(-90deg);     }
    .reg {         display: flex;         justify-content: flex-end;         width: 100%;     }
    .reg a {         font-size: 16px;         font-weight: 100;         margin-right: 2rem;     }
    .reg a:hover {         color: rgb(120, 120, 255);     } </style>
</html>

标签:node,flex,自定义,image,元素,height,width,margin,display
From: https://www.cnblogs.com/gaosj20210301/p/17462316.html

相关文章

  • 代码随想录算法训练营第十三天|● 239. 滑动窗口最大值 ● 347.前 K 个高频元素 ● 总
    239.滑动窗口最大值力扣题目链接(opensnewwindow)给定一个数组nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的k 个数字。滑动窗口每次只向右移动一位。返回滑动窗口中的最大值。进阶:你能在线性时间复杂度内解决此题吗?提示:1<......
  • 不能在foreach 循环中添加或删除元素
    迭代器在遍历map的时候,会先拿到modCount存起来然后遍历,在遍历的时候会判断当前modCount的值与我第一次进来存的值是否一样,不一样就报错如果在循环中添加或删除元素,是直接调用集合的add,remove方法【导致了modCount增加或减少】,但这些方法不会修改迭代实例中的expectedModCount,导......
  • 215. 数组中的第K个最大元素
    给定整数数组nums和整数k,请返回数组中第k个最大的元素。请注意,你需要找的是数组排序后的第k个最大的元素,而不是第k个不同的元素。你必须设计并实现时间复杂度为O(n)的算法解决此问题。 示例1:输入:[3,2,1,5,6,4],k=2输出:5示例 2:输入:[3,2,3,1,2,4,5,5......
  • Docker(二)自定义镜像
    Dockerfile是生成镜像脚本的应答文件,Dockerfile是比commit更强大的镜像编排方式Dockerfile语法格式:FROM:基础镜像MAINTAINER:镜像创建者信息EXPOSE:开放的端口ENV:设置变量ADD:复制文件到镜像RUN:制作镜像时执行的命令,可以有多个WORKDIR:定义容器默认工作目录CMD:容器启......
  • EasyExcel实战 自定义动态化导出excel
    1.Java操作excel表格,除了运用POI技术,阿里开发一个工具简易操作EasyExcel,接下来我们来实战操作下自定义动态化导出excel,自定义动态化为自定义标题,合并单元格引入pom<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId>......
  • CKEditor (Toolbar Definition)工具栏自定义配置
    CKEditor中的工具栏默认显示所有功能按钮。出于安全的原因、或者是简化的原因,需要对这个工具栏进行自定义设置。CKEditor工具栏自定义配置非常简单。编辑ckeditor目录下的的config.js文件,默认的是下面的工具栏代码,注意工具按钮是分组的,一个name内的大括号就是一个工具按钮......
  • 通过adb命令获取页面activity所有元素
    /***获取设备当前页面activity控件元素信息*@paramiDevice安卓设备信息*@return*/privateJSONArraygetDevicePageResource(IDeviceiDevice){longstartTime=System.currentTimeMillis();Stringcmd="adb-s"+......
  • 拓扑错误:自交。jts.geom.TopologyException: found non-noded intersection between L
    Thatbeingsaid,youwillwanttoensurethegeometriesarevalidbeforecomputingtheintersection,using polygon1.isValid() and polygon2.isValid().Thesampledatafor polygon2 isself-intersecting,sotheintersectionoperationfailswithcom.vividso......
  • ubuntu自定义服务
    ubuntu自定义服务,以SRT Server为例① 创建编辑sudovi/etc/systemd/system/srt.service1[Unit]2Description=SRTServerService3After=network.targetsyslog.target4Wants=network.target56[Service]7Type=simple8ExecStart=/home/vikin/sr......
  • 《深度剖析CPython解释器》19. Python类机制的深度解析(第三部分): 自定义类的底层实
    https://www.cnblogs.com/traditional/p/13593927.html楔子Python除了给我提供了很多的类之外,还支持我们定义属于自己的类,那么Python底层是如何做的呢?我们下面就来看看。自定义class老规矩,如果想知道底层是怎么做的,那么就必须要通过观察字节码来实现。classGirl:nam......