zoukankan      html  css  js  c++  java
  • jquery实现图片上传前本地预览功能

    HTML

    <img id="pic" src="" >
    <input id="upload" name="file" accept="image/*" type="file" style="display: none"/>
    

    CSS

    pic{
    100px;
    height:100px;
    border-radius:50% ;
    margin:20px auto;
    cursor: pointer;
    }
    

    JQ

    $(function() {
    $("#pic").click(function () {
    $("#upload").click(); //隐藏了input:file样式后,点击头像就可以本地上传
    $("#upload").on("change",function(){
    var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径,该路径不是图片在本地的路径,blob路径
    if (objUrl) {
    $("#pic").attr("src", objUrl) ; //将图片路径存入src中,显示出图片
    }
    });
    });
    });
     
    //建立一個可存取到該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 ;
    }
    

      

  • 相关阅读:
    学习日报
    学习日报
    学习日报
    学习日报
    《人月神话》读后感(第一二章)
    线程
    for each
    类的访问属性
    异常
    输入输出流
  • 原文地址:https://www.cnblogs.com/fan-bk/p/8664249.html
Copyright © 2011-2022 走看看