input预览上传图片

html代码

<input type="file" name="file"  id="file" >
<img src=""  id="img">

 

 

js代码

    $("#file").bind("change",function(){
            var url = null;
            if(window.createObjectURL != undefined) {
                url = window.createObjectURL(this.files[0]);
            } else if(window.URL != undefined) {
                url = window.URL.createObjectURL(this.files[0]);
            } else if(window.webkitURL != undefined) {
                url = window.webkitURL.createObjectURL(this.files[0]);
            }
            $("#img").attr("src",url);
        })

 

posted @ 2017-09-21 10:52  小盆友灬  阅读(156)  评论(0编辑  收藏  举报