html5中文学习网

您的位置: 首页 > 网站及特效实例 > javascript特效 » 正文

js实现上传文件添加和删除文件选择框_javascript技巧_

[ ] 已经帮助:人解决问题

本文这里给大家说个用javascript实现的很实用的功能,是在上传附件的时候,可以动态地添加和删除文件选择框,然后一次性上传。meeHTML5中文学习网 - HTML5先行者学习网

从理论上看,实现起来比较容易,但实际工作的时候还是遇到两个难点,这些难点归结起来都是一个原因造成的,那就是浏览器的兼容性。在脚本中要用到两个函数:insertAdjacentHTML和removeChild,而恰好这两个函数在Firefox下都不能正常使用。几乎花费了一天的时候,在网上搜索着解决的方法,还好被找到了,也让我大松一口气。meeHTML5中文学习网 - HTML5先行者学习网

具体两个函数是这样的:meeHTML5中文学习网 - HTML5先行者学习网
meeHTML5中文学习网 - HTML5先行者学习网

<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下都能正常使用,就要用另一个方法代替,最取上一层的父结点,然后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>

页面上这样引用:meeHTML5中文学习网 - HTML5先行者学习网

<div>    <input type="button" value="添加附件(Add)" onclick="addFile('myfile')">    </div>  <div id="myfile"></div>

在addFile函数中引用了另一个函数:insertHtml,这个函数主要是针对insertAdjacentHTML在firefox下无效的情况重写的,具体可以通过搜索insertAdjacentHTML找到。meeHTML5中文学习网 - HTML5先行者学习网

PS:清除file框的内容meeHTML5中文学习网 - HTML5先行者学习网

<input  type=file  name=ttt>     <input  type=button  onclick="ttt.select();document.execCommand('Delete');"   value=清除file框的内容>

第二个案例meeHTML5中文学习网 - HTML5先行者学习网

文件上传,删除效果图:meeHTML5中文学习网 - HTML5先行者学习网

刚开始:meeHTML5中文学习网 - HTML5先行者学习网

meeHTML5中文学习网 - HTML5先行者学习网

点击按钮“选择更多后”,可以添加很多选择文件:meeHTML5中文学习网 - HTML5先行者学习网

meeHTML5中文学习网 - HTML5先行者学习网

点击按钮“删除”后:meeHTML5中文学习网 - HTML5先行者学习网

meeHTML5中文学习网 - HTML5先行者学习网

实现代码:meeHTML5中文学习网 - HTML5先行者学习网

<!DOCTYPE html><html><head><meta charset="utf-8"><title>选择文件</title><style type="text/css">*{margin:0px;padding:0px;}div{margin:10px;}</style><script>//当点击添加更多时,增加一个DIV//先增加两个inputfunction 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>

代码瑕疵:!!!!在删除函数中,我选择的是删除第一个元素节点,而不是真正意义上的删除当前按钮,不知道怎么改善,求改正。meeHTML5中文学习网 - HTML5先行者学习网

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。meeHTML5中文学习网 - HTML5先行者学习网
meeHTML5中文学习网 - HTML5先行者学习网

(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助