问答详情
源自:9-22 编程练习

求解析这大段函数

var JCRUD=function(tb,colnum,saveAllBtn,add,ajaxSaver,allAjaxSaver,ajaxDeler){

    var del = tb.getElementsByTagName('a');

    var span = tb.getElementsByTagName('span');

    var ctr=[];/* 保存修改的tr对象 */

    var delEvent = function(){

        var dder = this.parentNode.parentNode;

        this.data=[];

        for(var i=0; i<dder.children.length-1; i++)

            this.data[i] = dder.children[i].children[0].firstChild.nodeValue;

        var tag = 0;

        for(var j=0; j<this.data.length; j++){

            if(this.data[j]!=='null'){/* 如果修改了单元格的默认值,这里也做相应修改 */

                tag=1;

                break;

            }

        }

        for(var k=0; k<ctr.length; k++) if(ctr[k]===dder) ctr.splice(k,1);

        dder.parentNode.removeChild(dder);

        if(tag==1) ajaxDeler.call(this);

    };

    var spanEvent = function(){/* 点击生成修改框 */

        var value = this.firstChild.nodeValue;

        var input = document.createElement('input');

        input.value = value;

        this.parentNode.appendChild(input);

        this.parentNode.removeChild(this);

        input.focus();

        input.onblur = function(){/* 失去焦点移除修改框 */

            var span = document.createElement('span');

            span.appendChild(document.createTextNode(this.value?this.value:'null'));/* 如果修改了单元格的默认值,这里也做相应修改 */

            span.onclick =spanEvent;

            this.parentNode.appendChild(span);

            this.parentNode.removeChild(this);

            if(value!=this.value){/* 如果内容改变生成保存按钮 */

                var tr = span.parentNode.parentNode

                    tds = tr.children;

                    btns = tds[colnum-1].getElementsByTagName('a');

                for(var i=0; i<btns.length; i++){

                    if(btns[i].firstChild.nodeValue!='保存'){

                        var saver = document.createElement('a');

                        saver.href="javascript:;";

                        saver.appendChild(document.createTextNode('保存'));

                    }else{

                        var saver = btns[i];

                    }

                }

                tds[tds.length-1].appendChild(saver);

                var tag=0;

                for(var k=0; k<ctr.length; k++)

                    if(ctr[k]===tr) tag=1;

                if(tag==-0) ctr.push(tr);

                saver.onclick=function(){/* 添加保存处理事件 */

                    this.data = [];

                    for(var i=0; i<tds.length-1; i++)

                        this.data[i] = this.parentNode.parentNode.children[i].children[0].firstChild.nodeValue;

                    ajaxSaver.call(this);

                    for(var i=ctr.length-1; i>=0; i--){

                        if(this.parentNode.parentNode===ctr[i]){

                            ctr.splice(i,1);

                        }

                    }

                    this.parentNode.removeChild(this);

                };

            }

        }

    };

    for(var i in del) del[i].onclick = delEvent;/* 给现在有元素添加事件 */

    for(var j in span) span[j].onclick = spanEvent;

    add.onclick = function(){

        var tbody = tb.children[0];

        var tr = document.createElement('tr');

        for(var j=0; j<colnum; j++){

            var td = document.createElement('td');

            if(j==(colnum-1)){

                var del = document.createElement('a');

                del.href='javascript:;';

                del.appendChild(document.createTextNode('删除'));

                del.onclick = delEvent;/* 给新加元素添加事件 */

                td.appendChild(del);

            }else{

                var span = document.createElement('span');

                span.appendChild(document.createTextNode('null'));/* 如果在添加时修改默认值,在这里修改的 */

                td.appendChild(span);

                span.onclick =spanEvent;

            }

            tr.appendChild(td);

        }

        tbody.appendChild(tr);

    };

    var getAllData = function(){/* 保存全部的数据解析 */

        var allData=[];

        for(var i=0; i<ctr.length; i++){

            allData[i]=[];

            for(var j=0; j<ctr[i].children.length-1; j++)

                allData[i].push(ctr[i].children[j].children[0].firstChild.nodeValue);

            ctr[i].children[colnum-1].removeChild(ctr[i].children[colnum-1].children[1]);

        }

        ctr=[];

        return allData;

    };

    saveAllBtn.onclick = function(){/* 添加保存全部数据保存事件 */

        this.allData = getAllData();

        if(this.allData.length){

            allAjaxSaver.call(this);

        }else{

            alert('No data!');

        }

    };

    window.onbeforeunload = function(){/* 刷新提示保存数据 */

        if(ctr.length){

            var y = confirm('数据还未保存,是否保存数据?')

            if(y){

                saveAllBtn.click();

            }

        }

    };

};


提问者:Guisoulmate 2016-02-18 15:06

个回答

  • 微积分2016
    2016-02-19 23:35:14

    你不能这么提问,让人摸不着头绪

    应该把重要的部分提出来

    这一一堆代码 怪吓吓人的

  • qq_演绎_0
    2016-02-18 16:58:13

    只有JS怎么看~~~