上传图预览

HTML部分

<img id="pscover" src="__PUBLIC__/Home/images/z_70.jpg" alt="" />
<input type="file" name="scover" id="scover" />
<img id="pqr" src="__PUBLIC__/Home/images/z_70.jpg" alt="" />
<input type="file" name="qr" id="qr"/>

JS部分(注意引入jQ)

window.onload = function(){
	var arr = new Array();
	arr["img[id='pscover']"] = "input[name='scover']";
	arr["img[id='pqr']"] = "input[name='qr']";
	for(var i in arr){
		void function(i){
			$(arr[i]).on("change",function(){
		 		var pic = this.files[0];
		 		pic_preview(pic,i);
			})		
		}(i)	 	
	}
	function pic_preview(pic,obj){
		var f = new FileReader();
		f.readAsDataURL(pic);
		f.onload = function (e){
			$(obj).attr("src",this.result).show();
		}
	}	
}

发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.