javascript错误 - 找不到节点:replaceChild

时间:2015-02-23 15:36:24

标签: javascript

我正在尝试在javascript中交换两个数组。当替换到最后一次迭代时,我得到了#34; NotFoundError:找不到节点"在parent.replaceChild(item2,item1)的调用中。请帮我解决我犯的错误。

    function sortTable(col){
            if($("loacte-resultsTable") == null || $("loacte-resultsTable") == undefined){  
            return false;
        }

    if (lastSort == col) {    
        // sorting on same column twice = reverse sort order
        absOrder ? absOrder = false : absOrder = true
    }
    else{
        absOrder = true
    }   
    lastSort = col;

    try{        
        var loacteResultsTable = $("loacte-resultsTable").getElementsByTagName("TBODY")[0];
        var loacteResultsTableTR = loacteResultsTable.getElementsByTagName("TR");
        allTR = loacteResultsTableTR;
    } catch (e) {   
        return false;       
    }

    // allTR now holds all the rows in the dataTable
    totalRows = allTR.length;
    colToSort = new Array();        //holds all the cells in the column to sort
    colArr = new Array();           //holds all the rows that correspond to the sort cell
    copyArr = new Array();          //holds an original copy of the sort data  to match to colArr
    resultArr = new Array();        //holds the output

    allNums = true
    allDates = true

    //store the original data
    //remember that the first row - [0] -  has column headings
    //so start with the second row - [1]
    //and load the contents of the cell into the array that will be sorted
    for (x=0; x < totalRows; x++){
        var data = setDataType(allTR[x].childNodes[col].innerText);
        if(typeof data!="undefined"){
            colToSort[x] = setDataType(allTR[x].childNodes[col].innerText);
        }else{
            colToSort[x] = setDataType(allTR[x].childNodes[col].textContent);
        }
        colArr[x] = allTR[x];           
    }

    //make a copy of the original
    for (x=0; x<colToSort.length; x++){
        copyArr[x] = colToSort[x];
    }   
    //sort the original data based on data type
    if (allNums){
        colToSort.sort(numberOrder);
    } else if (allDates){
        colToSort.sort(dateOrder);
    } else {    
        colToSort.sort(textOrder);
    }

    //match copy to sorted
    for(x=0; x<colToSort.length; x++) { 
        for(y=0; y<copyArr.length; y++) {
            if (colToSort[x] == copyArr[y]) {
                boolListed = false
                //search the ouput array to make sure not to use duplicate rows

                for(z=0; z<resultArr.length; z++) {
                    if (resultArr[z]==y) {
                        boolListed = true
                        break;
                    }
                }

                if (!boolListed){
                    resultArr[x] = y
                    break;
                }
            }
        }
    }

    //now display the results - it is as simple as swapping rows
    for (x=0; x<resultArr.length; x++) {    
        //allTR[x].swapNode(colArr[resultArr[x]])
        swapNodes(allTR[x],colArr[resultArr[x]]);
    }
    function swapNodes(item1,item2)
    {
        var itemtmp = item1.cloneNode(1);
        var parent = item1.parentNode;
        item2 = parent.replaceChild(itemtmp,item2);
        parent.replaceChild(item2,item1);
        parent.replaceChild(item1,itemtmp);
        itemtmp = null;
    }

}

对sortTable方法的调用来自synch()。这是用JS编写的UI部分:

function synch(){

    var loacteResults = $('loacte-results');
    var loacteResultsTable = $('loacte-resultsTable');

    tab = loacteResults.getElementsByTagName('TABLE')[0];
    loacteResults.removeChild(tab);

    var updatedResults =
                '<table id="loacte-resultsTable" cellspacing="0" cellpadding="3" border="1">' + 
                '<thead class="thead">' + 
                    '<tr>' + 
                        '<th>Site ID</th>' + 
                        '<th>Store Name</th>' + 
                        '<th>Agent Code</th>' + 
                        '<th>Address</th>' + 
                        '<th>City, State</th>' + 
                        '<th>Phone</th>' + 
                        '<th>Hours</th>' + 
                        '<th>Deleted</th>' + 
                        '<th width="65px;">Priority <img src="images/sort_up_down.gif" onclick="javascript:sortTable(8)" style="cursor: pointer;"/></th>' + 
                        '<th width="115px;">Est.Dist.(miles) <img src="images/sort_up_down.gif" onclick="javascript:sortTable(9)" style="cursor: pointer;"/></th>' + 
                    '</tr>' + 
                '</thead>' ;

    if(tr == '')
        updatedResults = updatedResults + '<tbody><tr><td colspan="10">No Stores to display</td></tr></tbody></table>';
    else                        
        updatedResults = updatedResults + '<tbody>' + tr +  '</tbody></table>';             

    loacteResults.innerHTML = updatedResults;

}

1 个答案:

答案 0 :(得分:0)

在第三个parent.replaceChild行中item1不再出现在页面上,因为它已被item2替换。

您的代码细分:

function swapNodes(item1,item2)
{
    var itemtmp = item1.cloneNode(1); //create a copy of item 1
    var parent = item1.parentNode; //set a reference to the parent
    item2 = parent.replaceChild(itemtmp,item2); //replace item2 with the copy of item1 and store the old child in item2
    parent.replaceChild(item2,item1); //replace item1 with item2
    parent.replaceChild(item1,itemtmp); //this line is redundant. <-- item1 no longer is on the page.
    itemtmp = null;
}