js实现多文件上传

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值