1. 程式人生 > >利用jquery實現inpufile控制元件上傳的圖片在網頁上顯示

利用jquery實現inpufile控制元件上傳的圖片在網頁上顯示

程式碼實現:

<!Doctype html>
<head>
<title>擼起袖子加油幹</title>
<meta charset='utf-8'>
</head>
<script src="http://www.jq22.com/jquery/jquery-3.3.1.js"></script>
<style>
.disapper{
	display:none;
}
</style>
<body>
    <input type="file" name=
"file"
id="file" accept="image/*" onchange="imgChange(this);"/>
<img id="img" src="" class='disapper' width="50" height="50" /> </body> <script type="text/javascript"> //選擇圖片顯示 function imgChange(obj) { //獲取點選的文字框 var file =document.getElementById("file"); var imgurl =window.URL.createObjectURL
(file.files[0]); var img =document.getElementById('img'); img.setAttribute('src',imgurl); // 修改img標籤src屬性值 $("img").removeClass('disapper'); };
</script> </html>

效果圖展示:
在這裡插入圖片描述