js實(shí)現(xiàn)上傳文件添加和刪除文件選擇框
本文這里給大家說個(gè)用javascript實(shí)現(xiàn)的很實(shí)用的功能,是在上傳附件的時(shí)候,可以動(dòng)態(tài)地添加和刪除文件選擇框,然后一次性上傳。
從理論上看,實(shí)現(xiàn)起來比較容易,但實(shí)際工作的時(shí)候還是遇到兩個(gè)難點(diǎn),這些難點(diǎn)歸結(jié)起來都是一個(gè)原因造成的,那就是瀏覽器的兼容性。在腳本中要用到兩個(gè)函數(shù):insertAdjacentHTML和removeChild,而恰好這兩個(gè)函數(shù)在Firefox下都不能正常使用。幾乎花費(fèi)了一天的時(shí)候,在網(wǎng)上搜索著解決的方法,還好被找到了,也讓我大松一口氣。
具體兩個(gè)函數(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下都能正常使用,就要用另一個(gè)方法代替,最取上一層的父結(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ù)中引用了另一個(gè)函數(shù):insertHtml,這個(gè)函數(shù)主要是針對(duì)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)容>
第二個(gè)案例
文件上傳,刪除效果圖:
剛開始:

點(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)擊添加更多時(shí),增加一個(gè)DIV
//先增加兩個(gè)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>
代碼瑕疵:?。。。≡趧h除函數(shù)中,我選擇的是刪除第一個(gè)元素節(jié)點(diǎn),而不是真正意義上的刪除當(dāng)前按鈕,不知道怎么改善,求改正。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Bootstrap基本組件學(xué)習(xí)筆記之input輸入框組(9)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本組件學(xué)習(xí)筆記之input輸入框組,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
js實(shí)現(xiàn)短信發(fā)送倒計(jì)時(shí)功能(正則驗(yàn)證)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)短信發(fā)送倒計(jì)時(shí)功能,包含正則驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
JavaScript與Java正則表達(dá)式寫法的區(qū)別介紹
這篇文章主要介紹了JavaScript與Java正則表達(dá)式寫法的區(qū)別介紹,需要的朋友可以參考下2017-08-08
JavaScript實(shí)現(xiàn)獲取img的原始尺寸的方法詳解
在微信小程序開發(fā)時(shí),它的image標(biāo)簽有一個(gè)默認(rèn)高度,這樣你的圖片很可能出現(xiàn)被壓縮變形的情況,所以就需要獲取到圖片的原始尺寸對(duì)image的寬高設(shè)置,本文就來分享一下JavaScript實(shí)現(xiàn)獲取img的原始尺寸的方法吧2023-03-03
JS動(dòng)畫效果打開、關(guān)閉層的實(shí)現(xiàn)方法
這篇文章主要介紹了JS動(dòng)畫效果打開、關(guān)閉層的實(shí)現(xiàn)方法,可實(shí)現(xiàn)js控制層從中心位置打開與關(guān)閉的功能,涉及javascript操作頁面元素的相關(guān)技巧,需要的朋友可以參考下2015-05-05

