JS實現(xiàn)可視化文件上傳
更新時間:2018年09月08日 14:33:33 作者:damys
這篇文章主要為大家詳細
介紹了JS實現(xiàn)可視化文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JS可視化文件上傳的具體代碼,供大家參考,具體內容如下
測試-Style
<style type="text/css"> .imgbox,.imgbox1 { position: relative; width: 200px; height: 180px; border: 1px solid #ddd; overflow: hidden; } .imgnum{ left: 0px; top: 0px; margin: 0px; padding: 0px; } .imgnum input,.imgnum1 input { position: absolute; width: 200px; height: 180px; opacity: 0; } .imgnum img,.imgnum1 img { width: 100%; height: 100%; } .close, .close1 { color: red; position: absolute; right: 10px; top: 0; display: none; } </style>
測試--HTML
<div id="img"> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img src="btn.png" class="img1" /> <img src="" class="img2" /> </div> </div> </div>
JS: 需要引入jquery
<script type="text/javascript"> $(function() { $(".filepath").on("change",function() { var srcs = getObjectURL(this.files[0]); //獲取路徑 $(this).nextAll(".img1").hide(); //this指的是input $(this).nextAll(".img2").show(); //fireBUg查看第二次換圖片不起做用 $(this).nextAll('.close').show(); //this指的是input $(this).nextAll(".img2").attr("src",srcs); //this指的是input $(this).val(''); //必須制空 $(".close").on("click",function() { $(this).hide(); //this指的是span $(this).nextAll(".img2").hide(); $(this).nextAll(".img1").show(); }) }) }) //關鍵代碼:getObjectURL return url function getObjectURL(file) { var url = null; if (window.createObjectURL != undefined) { url = window.createObjectURL(file) } else if (window.URL != undefined) { url = window.URL.createObjectURL(file) } else if (window.webkitURL != undefined) { url = window.webkitURL.createObjectURL(file) } return url }; //modify img $(function() { $("#img").on("change",".filepath1",function() { //alert($('.imgbox1').length); var srcs = getObjectURL(this.files[0]); //獲取路徑 alert(srcs); //this指的是input /* $(this).nextAll(".img22").attr("src",srcs); //this指的是input $(this).nextAll(".img22").show(); //fireBUg查看第二次換圖片不起做用*/ var htmlImg='<div class="imgbox1">'+ '<div class="imgnum1">'+ '<input type="file" class="filepath1" />'+ '<span class="close1">X</span>'+ '<img src="btn.png" class="img11" />'+ '<img src="'+srcs+'" class="img22" />'+ '</div>'+ '</div>'; $(this).parent().parent().before(htmlImg); $(this).val(''); //必須制空 $(this).parent().parent().prev().find(".img11").hide(); //this指的是input $(this).parent().parent().prev().find('.close1').show(); $(".close1").on("click",function() { $(this).hide(); //this指的是span $(this).nextAll(".img22").hide(); $(this).nextAll(".img11").show(); if($('.imgbox1').length>1){ $(this).parent().parent().remove(); } }) }) }) </script>
注:低版本IE不支持可視化
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 原生JS實現(xiàn)前端本地文件上傳
- js實現(xiàn)文件上傳功能 后臺使用MultipartFile
- JS+Struts2多文件上傳實例詳解
- vue.js 圖片上傳并預覽及圖片更換功能的實現(xiàn)代碼
- php+croppic.js實現(xiàn)剪切上傳圖片功能
- JavaScript使用享元模式實現(xiàn)文件上傳優(yōu)化操作示例
- JS實現(xiàn)圖片上傳多次上傳同一張不生效的處理方法
- js實現(xiàn)圖片上傳并預覽功能
- vue.js圖片轉Base64上傳圖片并預覽的實現(xiàn)方法
- Angularjs實現(xiàn)多圖片上傳預覽功能
- JavaScript實現(xiàn)異步圖像上傳功能
- SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能
- node.js自動上傳ftp的腳本分享
- JS實現(xiàn)的文件拖拽上傳功能示例
- Vue2.0實現(xiàn)調用攝像頭進行拍照功能 exif.js實現(xiàn)圖片上傳功能
- JS和Canvas實現(xiàn)圖片的預覽壓縮和上傳功能
- JavaScript代碼實現(xiàn)txt文件的上傳預覽功能
- js 實現(xiàn) input type="file" 文件上傳示例代碼
- js實現(xiàn)上傳圖片預覽的方法
- Servlet+Jsp實現(xiàn)圖片或文件的上傳功能具體思路及代碼
- 上傳圖片預覽JS腳本 Input file圖片預覽的實現(xiàn)示例
- JS實現(xiàn)上傳圖片的三種方法并實現(xiàn)預覽圖片功能
- js實現(xiàn)圖片上傳并正常顯示
- 一個簡單的jQuery插件ajaxfileupload.js實現(xiàn)ajax上傳文件例子
- JS中使用FormData上傳文件、圖片的方法
- 客戶端js判斷文件類型和文件大小即限制上傳大小
- js獲取上傳文件的絕對路徑實現(xiàn)方法
- 原生JS和jQuery版實現(xiàn)文件上傳功能
- 微信JSSDK上傳圖片
- 簡單實現(xiàn)js上傳文件功能
- JS文件上傳神器bootstrap fileinput詳解
- js實現(xiàn)上傳圖片之上傳前預覽圖片
- Javascript圖片上傳前的本地預覽實例
- 學習使用AngularJS文件上傳控件
- JS簡單實現(xiàn)文件上傳實例代碼(無需插件)
- 分享5個好用的javascript文件上傳插件
相關文章
JS截取字符串 subStr()、substring()、slice() 方法示例詳解
這篇文章主要介紹了JS截取字符串 subStr()、substring()、slice() 方法,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01