jQuery实现多张图片上传预览(不经过后端处理)


Posted in jQuery onApril 29, 2017

效果图:

jQuery实现多张图片上传预览(不经过后端处理)

图(1)

jQuery实现多张图片上传预览(不经过后端处理)

图(2)

代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery图片上传预览(不经过后端处理)</title>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<div>
 <img class="ImgPr"/>
 <input type="file" class="up" />
</div>
<div>
 <img class="ImgPr"/>
 <input type="file" class="up" />
</div>
<script>
jQuery.fn.extend({
   uploadPreview: function(opts) {
    var _self = this,
      _this = $(this);
    opts = jQuery.extend({
     Img: "ImgPr",
     Width: 100,
     Height: 100,
     ImgType: ["gif", "jpeg", "jpg", "bmp", "png"],
     Callback: function() {}
    }, opts || {});
    _self.getObjectURL = function(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
    };
    _this.change(function() {
     if (this.value) {
      if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {
       alert("选择文件错误,图片类型必须是" + opts.ImgType.join(",") + "中的一种");
       this.value = "";
       return false
      }
      //高版本Jquey使用 if ($.support.leadingWhitespace)
      if ($.support.leadingWhitespace) { //低版本jquery中使用$.browser.msie

       console.log(_self.getObjectURL(this.files[0]));
       try {
        _this.parent('div').find("." + opts.Img).attr('src', _self.getObjectURL(this.files[0]));
       } catch (e) {
        var src = "";
        var obj = _this.parent('div').find("." + opts.Img);
        var div = obj.parent("div")[0];
        _self.select();
        if (top != self) {
         window.parent.document.body.focus()
        } else {
         _self.blur()
        }
        src = document.selection.createRange().text;
        document.selection.empty();
        obj.hide();
        obj.parent("div").css({
         'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)',
         'width': opts.Width + 'px',
         'height': opts.Height + 'px'
        });
        div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src
       }
      } else {
       _this.parent('div').find("." + opts.Img).attr('src', _self.getObjectURL(this.files[0]))
      }
      opts.Callback()
     }
    })
   }
  });
  $(".up").click(function(){
   $(this).uploadPreview({
    Img: "ImgPr"
   });
  })

</script>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

jQuery 相关文章推荐
jQuery中animate()的使用方法及解决$(”body“).animate({“scrollTop”:top})不被Firefox支持的问题
Apr 04 jQuery
jQuery.ajax向后台传递数组问题的解决方法
May 12 jQuery
JS jQuery使用正则表达式去空字符的简单实现代码
May 20 jQuery
jQuery自动或手动图片切换效果
Oct 11 jQuery
jQuery Validate插件ajax方式验证输入值的实例
Dec 21 jQuery
jQuery实现ajax回调函数带入参数的方法示例
Jun 26 jQuery
jQuery实现购物车的总价计算和总价传值功能
Nov 28 jQuery
JQuery样式操作、click事件以及索引值-选项卡应用示例
May 14 jQuery
如何使用CSS3和JQuery easing 插件制作绚丽菜单
Jun 18 jQuery
Jquery属性的获取/设置及样式添加/删除操作技巧分析
Dec 23 jQuery
jQuery实现颜色打字机的完整代码
Mar 19 jQuery
jQuery实现鼠标拖动图片功能
Mar 04 jQuery
jQuery设置图片等比例缩小的方法
Apr 29 #jQuery
node.js+jQuery实现用户登录注册AJAX交互
Apr 28 #jQuery
jQuery使用eraser.js插件实现擦除、刮刮卡效果的方法【附eraser.js下载】
Apr 28 #jQuery
jquery实现图片上传前本地预览
Apr 28 #jQuery
jQuery实现jQuery-form.js实现异步上传文件
Apr 28 #jQuery
jQuery tip提示插件(实例分享)
Apr 28 #jQuery
jQuery自定义元素右键点击事件(实现案例)
Apr 28 #jQuery
You might like
电脑硬件及电脑配置知识大全
2020/03/17 数码科技
ajax+php打造进度条 readyState各状态
2010/03/20 PHP
完美利用Yii2微信后台开发的系列总结
2016/07/18 PHP
PHP7引入的&quot;??&quot;和&quot;?:&quot;的区别讲解
2019/04/08 PHP
关于编写性能高效的javascript事件的技术
2014/11/28 Javascript
javascript折半查找详解
2015/01/26 Javascript
jQuery添加删除DOM元素方法详解
2016/01/18 Javascript
jquery.form.js框架实现文件上传功能案例解析(springmvc)
2016/05/26 Javascript
探究Vue.js 2.0新增的虚拟DOM
2016/10/20 Javascript
js 获取本地文件及目录的方法(推荐)
2016/11/10 Javascript
微信小程序 开发之滑块视图容器(swiper)详解及实例代码
2017/02/22 Javascript
基于LayUI分页和LayUI laypage分页的使用示例
2017/08/02 Javascript
JS严格模式知识点总结
2018/02/27 Javascript
微信小程序云开发如何实现数据库自动备份实现
2019/08/16 Javascript
详解小程序云开发攻略(解决最棘手的问题)
2019/09/30 Javascript
videocapture库制作python视频高速传输程序
2013/12/23 Python
python 2.6.6升级到python 2.7.x版本的方法
2016/10/09 Python
Python中index()和seek()的用法(详解)
2017/04/27 Python
Python使用爬虫爬取静态网页图片的方法详解
2018/06/05 Python
python中struct模块之字节型数据的处理方法
2019/08/27 Python
python将数组n等分的实例
2019/12/02 Python
详解python 中in 的 用法
2019/12/12 Python
python中的itertools的使用详解
2020/01/13 Python
使用python客户端访问impala的操作方式
2020/03/28 Python
Python 如何批量更新已安装的库
2020/05/26 Python
iphoneX 适配客户端H5页面的方法教程
2017/12/08 HTML / CSS
Canvas 像素处理之改变透明度的实现代码
2019/01/08 HTML / CSS
为数据库创建索引都需要注意些什么
2012/07/17 面试题
工程力学硕士生的自我评价范文
2013/11/16 职场文书
实习报告评语
2014/04/26 职场文书
群众路线领导班子四风对照检查材料
2014/09/27 职场文书
技术负责人岗位职责
2015/02/10 职场文书
2015年毕业生自荐信范文
2015/03/24 职场文书
负责培养人意见
2015/06/05 职场文书
经营场所使用证明
2015/06/19 职场文书
SpringBoot中HttpSessionListener的简单使用方式
2022/03/17 Java/Android