当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 简单实现jQuery上传图片显示预览功能

简单实现jQuery上传图片显示预览功能

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了jquery上传图片显示预览的具体代码,供大家参考,具体内容如下

<!doctype html>
<html>
<head>
  <title>html5上传图片预览</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <script src="http://img9.tongzhuo100.com/js/jquery-1.7.2.min.js"></script>
  <style>
    .hide
    {
      display:none;
    }
  </style>
</head>
<body>
<h3>请选择图片文件:jpg/gif</h3>
<form name="form0" id="form0" >
  <input type="file" name="file0" id="file0" multiple="multiple" />
  <br><br><img src="" id="img0" width="120" class="hide">
</form>
<script>
  $("#file0").change(function(){
    var objurl = getobjecturl(this.files[0]) ;
    console.log("objurl = "+objurl) ;
    if (objurl) 
    {
      $("#img0").attr("src", objurl);
      $("#img0").removeclass("hide");
    }
  }) ;
  //建立一個可存取到該file的url
  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 ;
  }
</script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网