1. 程式人生 > >input預覽上傳圖片

input預覽上傳圖片

fun null files defined obj cnblogs div object style

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); })

input預覽上傳圖片