编辑图片时,可以实时显示上传的图片(预览)

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 ;

}

相关文章
相关标签/搜索