使用treewalker修改表

时间:2016-05-13 18:48:41

标签: javascript

我正在尝试使用树木行者在页面上找到一个表,插入一列,然后根据每行第一个单元格中的值填充该列中的单元格。我做了很多研究,但我一直都在失败。任何能让我朝着正确方向前进的点击都会非常感激。



document.ondblclick = addElement;
var i = 1;
function addElement () { 

    var allTextNodes = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT),
    // some temp references for performance
    tmptxt,
    tmpnode,
    // compile the RE and cache the replace string, for performance
    cakeRE = "Investment"
    replaceValue = "TestReplace";
    var allElementNodes = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT);

    // iterate through all text nodes
while (allTextNodes.nextNode()) {
    tmpnode = allTextNodes.currentNode;
    tmptxt = tmpnode.nodeValue;
    tmpnode.nodeValue = tmptxt.replace(cakeRE, replaceValue);
}

    // iterate through all Element nodes
while (allElementNodes.nextNode()) {
    tmpnode = allElementNodes.currentNode;

    //I know this is not even close to real code but I think it explains what i'm trying to do

    IF (tmpnode.tagvalue = "table")
    { 
    tmpnode.insertcolumn
    }
    If (tempnode.tagvalue = "tr" && Row.contains("Active"))
    {
    pupulate the cell from the previously created column with "Active Pending";
    }

    
}
}






function PopulateTable() {
    var table = document.getElementsByTagName("table")[0];
    var rows = table.getElementsByTagName("tr")[0];
    for(var i = 0; i< rows.length; i++ ) {
        var firstColumn = rows[0].getElementsByTagName("td")[0];
        //do your data aggregation here
        var newCell = document.createElement("td");
        newCell.innerHTML = "some data";//create row here
        rows[i].appendChild(newCell);
    }

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

我认为你可能会以错误的方式解决这个问题。在这种特殊情况下,我建议不要使用树木行者。相反,你可以做一些更简单的事情:

function PopulateTable() {
    var table = document.getElementsByTagName("table")[0];
    var rows = table.getElementsByTagName("tr")[0];
    for(var i = 0; i< rows.length; i++ ) {
        var firstColumn = rows[0].getElementsByTagName("td")[0];
        //do your data aggregation here
    }
    var newRow = document.createElement("tr");
    newRow.innerHTML = //create row here
    table.appendChild(newRow);
}