JS实现可视化文件上传


Posted in Javascript onSeptember 08, 2018

本文实例为大家分享了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不支持可视化

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
FormValidate 表单验证功能代码更新并提供下载
Aug 23 Javascript
js输入框邮箱自动提示功能代码实现
Dec 10 Javascript
我的Node.js学习之路(二)NPM模块管理
Jul 06 Javascript
JavaScript实现Java中Map容器的方法
Oct 09 Javascript
JavaScript中localStorage对象存储方式实例分析
Jan 12 Javascript
javascript遍历json对象的key和任意js对象属性实例
Mar 09 Javascript
详解webpack中的hash、chunkhash、contenthash区别
Jan 05 Javascript
vue2中使用less简易教程
Mar 27 Javascript
微信小程序实现即时通信聊天功能的实例代码
Aug 17 Javascript
微信小程序实现简单表格
Feb 14 Javascript
vue-cli3环境变量与分环境打包的方法示例
Feb 18 Javascript
小程序使用watch监听数据变化的方法详解
Sep 20 Javascript
js实现文件上传功能 后台使用MultipartFile
Sep 08 #Javascript
vue-router的HTML5 History 模式设置
Sep 08 #Javascript
vue移动端下拉刷新和上拉加载的实现代码
Sep 08 #Javascript
react 父子组件之间通讯props
Sep 08 #Javascript
js代码规范之Eslint安装与配置详解
Sep 08 #Javascript
vue弹窗插件实战代码
Sep 08 #Javascript
浅谈从React渲染流程分析Diff算法
Sep 08 #Javascript
You might like
《魔兽争霸3:重制版》翻车了?你想要的我们都没有
2019/11/07 魔兽争霸
php 数学运算验证码实现代码
2009/10/11 PHP
php5.5新数组函数array_column使用
2013/07/08 PHP
php实现专业获取网站SEO信息类实例
2015/04/02 PHP
PHP模板引擎Smarty内建函数section,sectionelse用法详解
2016/04/11 PHP
PHP生成唯一ID之SnowFlake算法
2016/12/17 PHP
一个简单的JavaScript数据缓存系统实现代码
2010/10/24 Javascript
javascript基础知识大集锦(一) 推荐收藏
2011/01/13 Javascript
20款超赞的jQuery插件 Web开发人员必备
2011/02/26 Javascript
Mac地址验证的javascript代码
2013/11/09 Javascript
node.js中的fs.write方法使用说明
2014/12/15 Javascript
swtich/if...else的替代语句
2015/08/16 Javascript
浅谈DOCTYPE对$(window).height()取值的影响
2016/07/21 Javascript
easyui form validate总是返回false的原因及解决方法
2016/11/07 Javascript
微信小程序 本地存储及登录页面处理实例详解
2017/01/11 Javascript
jQuery插件DataTables分页开发心得体会
2017/08/22 jQuery
利用原生js实现html5小游戏之打砖块(附源码)
2018/01/03 Javascript
vue.js中$set与数组更新方法
2018/03/08 Javascript
了解javascript中变量及函数的提升
2019/05/27 Javascript
解决vue-cli webpack打包开启Gzip 报错问题
2019/07/24 Javascript
JavaScript数值类型知识汇总
2019/11/17 Javascript
基于Electron实现桌面应用开发代码实例
2020/07/07 Javascript
python2.7删除文件夹和删除文件代码实例
2013/12/18 Python
Django中URL视图函数的一些高级概念介绍
2015/07/20 Python
python 生成器协程运算实例
2017/09/04 Python
浅谈Pycharm调用同级目录下的py脚本bug
2018/12/03 Python
用Python画一个LinkinPark的logo代码实例
2019/09/10 Python
Python全局变量与global关键字常见错误解决方案
2020/10/05 Python
世界上最大的乐器零售商:Guitar Center
2017/11/07 全球购物
亚洲在线旅行门户网站:Expedia.com.hk(智游网)
2020/04/14 全球购物
舞蹈教育学专业求职信
2014/06/29 职场文书
亲子运动会的活动方案
2014/08/17 职场文书
党员批评与自我批评发言材料
2014/10/14 职场文书
对领导班子的意见和建议
2015/06/08 职场文书
Java内存模型之happens-before概念详解
2021/06/13 Java/Android
Python中itertools库的四个函数介绍
2022/04/06 Python