編輯圖片時,可以實時顯示上傳的圖片(預覽)

html代碼:html

js代碼:web

var string ="";chrome

for (var i = 0; i < 5; i++)dom

{ide

  string += Math.floor(Math.random() * 10)this

}url

$('.wap_store_img').each(function(){firefox

  this.src = this.alt + '?' + string;htm

});blog

$("#store_img").on("change",function(){

  var objUrl = getObjectURL(this.files[0]) ; //獲取圖片的路徑,該路徑不是圖片在本地的路徑

   if (objUrl) {

    $(".hide_img img").attr("src", objUrl) ; //將圖片路徑存入src中,顯示出圖片

  }

});

//創建一個可存取到該file的url

function getObjectURL(file) {

  var url = null ;

  if (window.createObjectURL!=undefined) {   // basic

     url = window.createObjectURL(file) ;

  }

  else if (window.URL!=undefined) {              // mozilla(firefox)

     url = window.URL.createObjectURL(file) ;

   }

  else if (window.webkitURL!=undefined) {   // webkit or chrome

    url = window.webkitURL.createObjectURL(file) ;

   }

   return url ;

}

相關文章
相關標籤/搜索