js實(shí)現(xiàn)上傳文件添加和刪除文件選擇框
本文這里給大家說個用javascript實(shí)現(xiàn)的很實(shí)用的功能,是在上傳附件的時候,可以動態(tài)地添加和刪除文件選擇框,然后一次性上傳。
從理論上看,實(shí)現(xiàn)起來比較容易,但實(shí)際工作的時候還是遇到兩個難點(diǎn),這些難點(diǎn)歸結(jié)起來都是一個原因造成的,那就是瀏覽器的兼容性。在腳本中要用到兩個函數(shù):insertAdjacentHTML和removeChild,而恰好這兩個函數(shù)在Firefox下都不能正常使用。幾乎花費(fèi)了一天的時候,在網(wǎng)上搜索著解決的方法,還好被找到了,也讓我大松一口氣。
具體兩個函數(shù)是這樣的:
<script type="text/javascript"> //刪除文件選擇框 function removeFile(id) { var new_tr = id.parentNode; try { //new_tr.removeNode(true); // just ie , not w3c; // other idea var tmp = new_tr.parentNode; // 為了在ie和firefox下都能正常使用,就要用另一個方法代替,最取上一層的父結(jié)點(diǎn),然后remove. tmp.removeChild(new_tr); } catch(e) {} } //添加文件選擇框 function addFile(id) { var str = '<div><input type="file" runat="server" name="file" onKeyDown="this.blur();" oncontextmenu="return false" /><input type="button" value="刪除" style="height:22px;" onclick="removeFile(this)" /></div>' insertHtml("beforeend",document.getElementById(id),str); } </script>
頁面上這樣引用:
<div> <input type="button" value="添加附件(Add)" onclick="addFile('myfile')"> </div> <div id="myfile"> </div>
在addFile函數(shù)中引用了另一個函數(shù):insertHtml,這個函數(shù)主要是針對insertAdjacentHTML在firefox下無效的情況重寫的,具體可以通過搜索insertAdjacentHTML找到。
PS:清除file框的內(nèi)容
<input type=file name=ttt> <input type=button onclick="ttt.select();document.execCommand('Delete');" value=清除file框的內(nèi)容>
第二個案例
文件上傳,刪除效果圖:
剛開始:
點(diǎn)擊按鈕“選擇更多后”,可以添加很多選擇文件:
點(diǎn)擊按鈕“刪除”后:
實(shí)現(xiàn)代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>選擇文件</title> <style type="text/css"> *{ margin:0px; padding:0px; } div{ margin:10px; } </style> <script> //當(dāng)點(diǎn)擊添加更多時,增加一個DIV //先增加兩個input function addFile(){ var fragment=document.createDocumentFragment(); var divNode=document.getElementById("container"); var newDiv=document.createElement("div"); newDiv.setAttribute("id","file"); fragment.appendChild(newDiv); var newInput=document.createElement("input"); newInput.setAttribute("type","file"); newInput.setAttribute("name","選擇文件"); newDiv.appendChild(newInput); var newInput=document.createElement("input"); newInput.setAttribute("type","button"); newInput.setAttribute("value","刪除"); newInput.setAttribute("onclick","delFile()"); newInput.setAttribute("id","1"); newDiv.appendChild(newInput); divNode.appendChild(fragment); } function delFile(){ var divNode=document.getElementById("container"); divNode.removeChild(divNode.firstElementChild); } </script> </head> <body> <input type="button" value="選擇更多" onclick="addFile()"/> <div id="container"> <div id="file"> <input type="file" name="選擇文件"/> <input type="button" value="刪除" onclick="delFile()" /> </div> </div> </body> </html>
代碼瑕疵:!?。?!在刪除函數(shù)中,我選擇的是刪除第一個元素節(jié)點(diǎn),而不是真正意義上的刪除當(dāng)前按鈕,不知道怎么改善,求改正。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
原生JavaScript實(shí)現(xiàn)AJAX、JSONP
本篇文章將會講解原生JavaScript如何實(shí)現(xiàn)簡單的AJAX,還有跨域請求JSONP。具有很好的參考價值。下面跟著小編一起來看下吧2017-02-02javascript克隆元素樣式的實(shí)現(xiàn)代碼
這是一個實(shí)驗(yàn)用的玩意,它可以克隆指定元素的最終樣式,并包裝成一個css類,它還可以證明Oprea 11.10 是個混球2011-10-10javascript實(shí)現(xiàn)數(shù)組最大值和最小值的6種方法
比較數(shù)組中數(shù)值的大小是比較常見的操作,本文主要介紹了javascript實(shí)現(xiàn)數(shù)組最大值和最小值的6種方法,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Javascript設(shè)計(jì)模式之觀察者模式的多個實(shí)現(xiàn)版本實(shí)例
這篇文章主要介紹了Javascript設(shè)計(jì)模式之觀察者模式的多個實(shí)現(xiàn)版本實(shí)例,本文給出3種實(shí)現(xiàn)版本代碼,同時給出了Jquery實(shí)現(xiàn)版本,需要的朋友可以參考下2015-03-03