JQ input 文件上传并预览 选择图片后,在页面上预览图片,页面渲染

有时候,我们更新界面信息时会遇到,选择图片后,并不能预览图片,必须提交数据库后才能在页面上显示出来,这样我们该怎样办呢?

起始jq为我们提供了两个方法change()和URL.createObjectURL() 直接掉用就行了,废话少说,直接上代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./jquery.min.js"></script>
    <script>
    $(function(){
        $('.file').change(function(){
        $filePath=URL.createObjectURL(this.files[0]);
        $('img').attr('src',$filePath);
        })
    })
    </script>
</head>
<body>
    <input type="file" class="file">
    <br>
    <img src="" alt="">
</body>
</html>


猜你喜欢

转载自blog.csdn.net/qq_37011771/article/details/80564508