js实现多文件上传
2016-07-08 18:30
399 查看
js实现多文件上传
HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="util.js"></script>
<script>
//多文件上传 例子 练习dom操作 和动态事件绑定
function addFile(){
//获取新添加的文件的input 的标签的容器
var con=$("#container");
//创建我们的file文件域
var fileInp=document.createElement("input");
//设置input 的类型为file
fileInp.type="file";
//创建我们的删除按钮
var butInp=document.createElement("input");
//设置类型和 按钮的名字
butInp.type="button";
butInp.value="删除";
//创建一个换行
var br=document.createElement("br");
//从前面正向添加
con.insertBefore(br,con.firstChild);
con.insertBefore(butInp,con.firstChild);
con.insertBefore(fileInp,con.firstChild);
//注释的部分为尾部添加 前面的代码注释起来 执行下面即可
// con.appendChild(fileInp);
// con.appendChild(butInp);
// con.appendChild(br);
butInp.onclick=function(){
con.removeChild(br);
con.removeChild(butInp);
con.removeChild(fileInp);
}
}
</script>
</head>
<body>
<form action="test.html" method="get">
<input type="file" />
<input type="button" value="添加" onclick="addFile()" />
<div id="container" >
</div>
</form>
</body>
</html>
js代码:
/**
*
* @param idOrName 如果传入id 前面加上# 如果传入name 直接传入
* @returns {*} 返回元素节点 如果没找到 返回null
*/
function $(idOrName){
var obj=null;
if(idOrName){
if(idOrName.charAt(0)=="#"){
obj=document.getElementById(idOrName.substring(1));
}else{
obj=document.getElementsByName(idOrName);
}
}
return obj;
}
/**
*
* @param parentNode 父节点
* @returns {Array} 所有的元素子节点
*/
function getChildNodes(parentNode){
var childs=parentNode.childNodes;
var newChilds=[];
for(var i=0;i<childs.length;i++){
if(childs[i].nodeType==1){
newChilds.push(childs[i]);
}
}
return newChilds;
}
/**
*
* @param parentNode 父节点
* @returns {*|Node} 第一个元素节点
*/
function getFirstChild(parentNode){
var firstChild=parentNode.firstChild;
if(firstChild.nodeType==3){
firstChild=firstChild.nextSibling;
}
return firstChild;
}
/**
*
* @param parentNode 父节点
* @returns {*|Node} 最后一个元素节点
*/
function getLastChild(parentNode){
var lastChild=parentNode.lastChild;
if(lastChild.nodeType==3){
lastChild=lastChild.previousSibling;
}
return lastChild;
}
/**
*
* @param node 元素节点
* @returns {*|Node} 返回下一个兄弟元素节点
*/
function getNextSibling(node){
var nextNode=node.nextSibling;
if(nextNode.nodeType==3){
nextNode=nextNode.nextSibling;
}
return nextNode;
}
/**
*
* @param node 元素节点
* @returns {*|Node} 返回前一个兄弟元素节点
*/
function getPreviousSibling(node){
var preNode=node.previousSibling;
if(preNode.nodeType==3){
preNode=preNode.previousSibling;
}
return preNode;
}
HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="util.js"></script>
<script>
//多文件上传 例子 练习dom操作 和动态事件绑定
function addFile(){
//获取新添加的文件的input 的标签的容器
var con=$("#container");
//创建我们的file文件域
var fileInp=document.createElement("input");
//设置input 的类型为file
fileInp.type="file";
//创建我们的删除按钮
var butInp=document.createElement("input");
//设置类型和 按钮的名字
butInp.type="button";
butInp.value="删除";
//创建一个换行
var br=document.createElement("br");
//从前面正向添加
con.insertBefore(br,con.firstChild);
con.insertBefore(butInp,con.firstChild);
con.insertBefore(fileInp,con.firstChild);
//注释的部分为尾部添加 前面的代码注释起来 执行下面即可
// con.appendChild(fileInp);
// con.appendChild(butInp);
// con.appendChild(br);
butInp.onclick=function(){
con.removeChild(br);
con.removeChild(butInp);
con.removeChild(fileInp);
}
}
</script>
</head>
<body>
<form action="test.html" method="get">
<input type="file" />
<input type="button" value="添加" onclick="addFile()" />
<div id="container" >
</div>
</form>
</body>
</html>
js代码:
/**
*
* @param idOrName 如果传入id 前面加上# 如果传入name 直接传入
* @returns {*} 返回元素节点 如果没找到 返回null
*/
function $(idOrName){
var obj=null;
if(idOrName){
if(idOrName.charAt(0)=="#"){
obj=document.getElementById(idOrName.substring(1));
}else{
obj=document.getElementsByName(idOrName);
}
}
return obj;
}
/**
*
* @param parentNode 父节点
* @returns {Array} 所有的元素子节点
*/
function getChildNodes(parentNode){
var childs=parentNode.childNodes;
var newChilds=[];
for(var i=0;i<childs.length;i++){
if(childs[i].nodeType==1){
newChilds.push(childs[i]);
}
}
return newChilds;
}
/**
*
* @param parentNode 父节点
* @returns {*|Node} 第一个元素节点
*/
function getFirstChild(parentNode){
var firstChild=parentNode.firstChild;
if(firstChild.nodeType==3){
firstChild=firstChild.nextSibling;
}
return firstChild;
}
/**
*
* @param parentNode 父节点
* @returns {*|Node} 最后一个元素节点
*/
function getLastChild(parentNode){
var lastChild=parentNode.lastChild;
if(lastChild.nodeType==3){
lastChild=lastChild.previousSibling;
}
return lastChild;
}
/**
*
* @param node 元素节点
* @returns {*|Node} 返回下一个兄弟元素节点
*/
function getNextSibling(node){
var nextNode=node.nextSibling;
if(nextNode.nodeType==3){
nextNode=nextNode.nextSibling;
}
return nextNode;
}
/**
*
* @param node 元素节点
* @returns {*|Node} 返回前一个兄弟元素节点
*/
function getPreviousSibling(node){
var preNode=node.previousSibling;
if(preNode.nodeType==3){
preNode=preNode.previousSibling;
}
return preNode;
}
相关文章推荐
- JS问题解决(一)
- JS中的prototype
- 《Javascript高级程序设计》读书笔记之——基本包装类型
- JavaScript 资源大全中文版
- JavaScript中的Function类型浅析
- Javascript高级程序设计-问答模式
- echart.js的使用与API
- 一个调用网易云音乐API 下载想要歌曲的几行字
- javascript基础:事件4事件绑定及深入
- 常用效果(六) : 格式化json 日志
- JavaScript学习之变量
- js获取url传递参数,js获取url?号后面的参数
- JavaScript网页编程之------浏览器对象模型(BOM)
- js第四天总结
- js中的关系操作符
- webstorm支持ES6语法详解
- Start My JS Road:2
- javascript sort函数探究
- javascript sort函数探究
- jsonp