js實(shí)現(xiàn)上傳圖片預(yù)覽功能思路是獲取上傳圖片本地路徑,再加載到頁(yè)面中實(shí)現(xiàn)上傳預(yù)覽
HTML代碼
<div class="upload"> <input type="button" class="btn" onclick="browerfile.click()" value="上傳"> <input type="file" id="browerfile" style="display: none;" class="test"> <div class="img_center"> <img src="" class="img1-img"> </div> </div>
實(shí)現(xiàn)功能的js代碼
//獲取圖片路勁的方法,兼容多種瀏覽器,通過(guò)createObjectURL實(shí)現(xiàn)function getObjectURL(file){ var url = null; if(window.createObjectURL != undefined){ url = window.createObjectURL(file);//basic }else if(window.URL != undefined){ url = window.URL.createObjectURL(file); }else if(window.webkitURL != undefined){ url = window.webkitURL.createObjectURL(file); } return url;}//實(shí)現(xiàn)功能代碼$(function(){ $("#browerfile").change(function(){ var path = browerfile.value; var objUrl = getObjectURL(this.files[0]); if(objUrl){ $('.img1-img').attr("src",objUrl); } })})以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答