javascript - 下一页 sibling 问题

标签 javascript dom nodes siblings

好吧,这让我整晚都快要死了,我的意思是我现在已经在这段代码上工作了至少 8 个小时。这是什么问题啊,arggggg。

我正在尝试更新所有 <span id="column_[row index number]_[column index number]_[layout position number]">将其加一直到下一个 id="row_[row index number]" tr element ,它应该搜索的 tr 元素的 id 为 tr_[row index number]_[column index number]_[layout position number]但出于某些天知道是什么原因,它给我带来了问题。它正在更新相同的<span>标记 2x,这会将其从所需值更改为比应有值多 1 的值...只有 1 <span> firstChild <td> element 内的标签<tr> elements 中的每一个。我只是不明白为什么它只为其中 1 个设置 2x,而且它似乎是随机的或其他什么。 arggggg。

只有 1 <span> <td id="tdcolumn_[row index number]_[column index number]_[layout position number]> 中的元素标签,但由于某种原因,它调用同一个标签两次。它应该只调用一次。 arggg。我不明白为什么??

这是我的代码,请有人帮助我...

// Reorder all columns, if any, in the other rows after this 1.
if (aRowId != 0 && lId.indexOf("tr_" + aRowId) == 0 && rowComplete != aRowId)
{
    var tempTr = lTable.childNodes[i].childNodes[p];


    while(tempTr.nodeType == 1 && tempTr.nextSibling != null)
    {
        var tempId = tempTr.getAttribute("id");

        if (!tempId) continue;

        if (tempId.indexOf("row_") == 0)
        {
            // All done this row, set it to completed!
            rowComplete = aRowId;
            break;
        }

        if (tempTr.hasChildNodes)
        {

            var doneChilds = false;

            // grab the id where tdcolumn_{aRowId}.indexOf = 0.
            for (fcTd = 0; fcTd<tempTr.childNodes.length; fcTd++)
            {
                if (tempTr.childNodes[fcTd].nodeName == '#text') continue;

                var tempfcId = tempTr.childNodes[fcTd].getAttribute("id");

                if (!tempfcId) continue;

                if (tempfcId.indexOf("tdcolumn_" + aRowId) != 0) continue;

                // looping through the children in the <td> element here.
                if (tempTr.childNodes[fcTd].hasChildNodes)
                {
                    for (x = tempTr.childNodes[fcTd].childNodes.length-1; x>0; x--)
                    {
                        if (tempTr.childNodes[fcTd].childNodes[x].nodeName == '#text') continue;

                        var tempSpanId = tempTr.childNodes[fcTd].childNodes[x].getAttribute("id");

                        if (!tempSpanId) continue;

                        if (tempSpanId.indexOf("column_") != 0) 
                            continue;

                        // alert(tempSpanId);

                        alert(tempTr.childNodes[fcTd].childNodes[x].nodeName);

                        var tSpanId = new Array();
                        tSpanId = tempSpanId.split("_");

                        if (currColumnId == 0)
                        {
                            currColumnId = parseInt(tSpanId[1]);
                            var incCol = currColumnId;  
                        }

                        incCol++;

                        // alert("currColumnId = " + currColumnId + "\n\ntSpanId[1] = " + tSpanId[1] + "\n\nincCol = " + incCol);

                        // Set the new Id's and Text, after which we can exit the for loop.
                        tempTr.childNodes[fcTd].childNodes[x].setAttribute("id", "column_" + incCol);
                        tempTr.childNodes[fcTd].childNodes[x].setAttribute("class", "dp_edit_column");
                        tempTr.childNodes[fcTd].childNodes[x].innerHTML = oColumnText + " " + incCol;
                        // tempTr.childNodes[fcTd].setAttribute("id", "tdcolumn_" + aRowId + "_" + (parseInt(tSpanId[1])+1) + "_" + tSpanId[3]);

                        doneChilds = true;

                        break;
                    }
                }
                else
                    continue;

                if (doneChilds == true)
                    continue;
            }
        }
        else
            continue;

        tempTr = tempTr.nextSibling;
    }
}

请帮助我,谢谢:)

最佳答案

虽然我认为如果没有相关的 HTML 部分就无法解决您的问题,但我在您的代码中至少看到一个错误:

if (doneChilds = true)

它的计算结果始终为true。它应该是:

if (doneChilds)

顺便说一句,您在这里不需要 getAttribute:

var tempfcId = tempTr.childNodes[fcTd].getAttribute("id");

只需使用:

var tempfcId = tempTr.childNodes[fcTd].id;

切勿使用 setAttribute 设置类名,如下所示:

tempTr.childNodes[fcTd].childNodes[x].setAttribute("class", "dp_edit_column");

用途:

tempTr.childNodes[fcTd].childNodes[x].className = "dp_edit_column";

(该行上面的行也是如此,设置元素的 id)。

关于javascript - 下一页 sibling 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2891152/

相关文章:

javascript - 如何将异步命令发送到 VSCode 编辑器的扩展

javascript - 更改输入字段或文本区域中选定的文本

javascript - 如何使 Angular ui-router 父状态在状态更改时始终执行 Controller 代码?

javascript - 在 Rails 应用程序中改进不显眼的 javascript(并可能使用 CoffeeScript)

css - YUI - 将填充样式设置为拖动节点

javascript - 无法读取未定义的属性 'replace'(在克隆的 html 表上)

javascript - Node.js 中的 Array.forEach 是异步的吗?

python - 为什么这个 while 循环会结束? node5 不是 NoneType。 (遍历链表)

java - 在 Neo4j 索引中创建第 128 个节点后,无法访问更多节点

php - 如何重用一个 html head 部分?