js实现上传图片选择图片后的预览功能

普通的上传图片选择图片后并不知道自己选择的什么图片,那么通过js我们可以做出预览效果,这样就知道选择的什么图片,以免误上传!

<!DOCTYPE html>
    <head>
        <title>js上传图片--预览图片</title>
        <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    </head>
     
    <body>
        <form>
            <input type="file" onChange="preview(this)"/>
            <img src="" id="previewimg" width="50px" height="40px"/>
        </form>
        <script type="text/javascript">
            function preview(obj){
                var img = document.getElementById("previewimg");
                img.src = window.URL.createObjectURL(obj.files[0]);
            }
        </script>
    </body>
</html>

评论 抢沙发

表情
  1. 来自四川成都的用户(2017-06-08 03:35)
    欢迎各位大神留言指导交流,或添加博主微信(右上角关注本站扫一扫添加),添加QQ,加群分享交流!