js實現(xiàn)圖片上傳即時顯示效果
前言
h5實訓時實現(xiàn)的一個圖片上傳即時顯示的效果,如下圖所示
正文
Html代碼
<form action="" method="POST" role="form"> <div class="form-group"> <label for="touxiang" >頭像上傳:</label> <input type="file" id="headPhoto" name="headPhoto" /> <div ><img id="imag" src="img/up.png" alt="" style="height:5rem;width: 5rem;"></div> </div> </form>
js腳本代碼
<script> /*顯示上傳的圖片*/ function getObjectURL(file) { var url = null ; if (window.createObjectURL!=undefined) { url = window.createObjectURL(file) ; } else if (window.URL!=undefined) { url = window.URL.createObjectURL(file) ; } else if (window.webkitURL!=undefined) { url = window.webkitURL.createObjectURL(file) ; } return url ; } $('#headPhoto').change(function() { var eImg=$('#imag'); eImg.attr('src', getObjectURL($(this)[0].files[0])); $(this).after(eImg); }); </script>
window.URL.createObjectURL方法
創(chuàng)建一個新的對象URL,該對象URL可以代表某一個指定的File對象或Blob對象.
語法:
objectURL = window.URL.createObjectURL(blob);
blob參數(shù)是一個File對象或者Blob對象.
objectURL是生成的對象URL.通過這個URL,可以獲取到所指定文件的完整內(nèi)容.
完整代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>圖片上傳</title> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <link href=https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css rel="stylesheet"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <form action="" method="POST" role="form"> <div class="form-group"> <label for="touxiang" >頭像上傳:</label> <input type="file" id="headPhoto" name="headPhoto" /> <div ><img id="imag" src="img/up.png" alt="" style="height:5rem;width: 5rem;"></div> </div> </form> </body> <script> /*顯示上傳的圖片*/ function getObjectURL(file) { var url = null ; if (window.createObjectURL!=undefined) { url = window.createObjectURL(file) ; } else if (window.URL!=undefined) { url = window.URL.createObjectURL(file) ; } else if (window.webkitURL!=undefined) { url = window.webkitURL.createObjectURL(file) ; } return url ; } $('#headPhoto').change(function() { var eImg=$('#imag'); eImg.attr('src', getObjectURL($(this)[0].files[0])); $(this).after(eImg); }); </script> </html>
參考:鏈接
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript cookie與session的使用及區(qū)別深入探究
這篇文章主要介紹了Java中Cookie和Session詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-10-10Javascript將數(shù)字轉(zhuǎn)化成為貨幣格式字符串
這篇文章主要介紹Javascript將數(shù)字轉(zhuǎn)化成為貨幣格式字符串的方法,通俗易懂,需要的朋友可以參考下。2016-06-06js+canvas實現(xiàn)兩張圖片合并成一張圖片的方法
這篇文章主要介紹了js+canvas實現(xiàn)兩張圖片合并成一張圖片的方法,結(jié)合實例形式分析了JavaScript結(jié)合HTML5 canvas實現(xiàn)圖片合并的操作技巧,并附帶了Java圖片合并的實現(xiàn)方法,需要的朋友可以參考下2019-11-11js中的數(shù)組轉(zhuǎn)樹型結(jié)構(gòu)方式
這篇文章主要介紹了js中的數(shù)組轉(zhuǎn)樹型結(jié)構(gòu)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06js實現(xiàn)點擊鏈接后延遲3秒再跳轉(zhuǎn)的方法
這篇文章主要介紹了js實現(xiàn)點擊鏈接后延遲3秒再跳轉(zhuǎn)的方法,通過javascript的setTimeout方法實現(xiàn)延遲跳轉(zhuǎn)的功能,需要的朋友可以參考下2015-06-06