next兄弟姐妹问题,应该很简单

发布于 2024-09-02 23:30:45 字数 674 浏览 1 评论 0原文

好吧,我正在尝试掌握 JS 中的 nextSibling 函数。这是我在以下代码中遇到的问题...

var fromRow = document.getElementById("row_1");

while(fromRow.nodeType == 1 && fromRow.nextSibling != null)
{
    var fRowId = fromRow.id;
    if (!fRowId) continue;

    // THIS ONLY gets done once and alerts "row_1" ONLY :(
    alert(fRowId);

    fromRow = fromRow.nextSibling;
}

好的,有人可以告诉我这段代码有什么问题吗?正如我所看到的,这个 document.getElementById("row_1"); 元素旁边肯定有同级元素,并且它们都有 id 属性,那么为什么它没有获取同级元素的 id 属性??我不明白。

row_1 是一个 TR 元素,我需要获取此表中它旁边的 TR 元素,但由于某种原因,它只获取我已经可以使用 document.getElementById 获取第 1 个元素,arggg。

谢谢大家:)

Ok, I'm trying to come to grips with this nextSibling function in JS. Here's my issue within the following code...

var fromRow = document.getElementById("row_1");

while(fromRow.nodeType == 1 && fromRow.nextSibling != null)
{
    var fRowId = fromRow.id;
    if (!fRowId) continue;

    // THIS ONLY gets done once and alerts "row_1" ONLY :(
    alert(fRowId);

    fromRow = fromRow.nextSibling;
}

Ok can someone please tell me what is wrong with this code? There are siblings next to this document.getElementById("row_1"); element for sure as I can see them, and they all have id attributes, so why is it not getting the id attributes of the siblings?? I don't get it.

row_1 is a TR element, and I need to get the TR elements next to it within this table, but for some reason, it only gets the 1 element that I can already get using document.getElementById, arggg.

Thanks guys :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷夜 2024-09-09 23:30:45

尝试:

var fromRow = document.getElementById("row_1");

while(fromRow !== null)
{
    var fRowId = fromRow.id;
    if (!fRowId || fromRow.nodeType != 1) {
        fromRow = fromRow.nextSibling;
        continue;
    }

    // THIS ONLY gets done once and alerts "row_1" ONLY :(
    alert(fRowId);
    fromRow = fromRow.nextSibling;
}

虽然 fromRow.nextSibling != null 会在倒数第二次迭代时停止,因为您已经在最后将 fromRow 设置为其 nextSibling 。另外,如果下一个节点不是元素,您不一定要停止,您只想在可能的情况下移动到下一个节点。最后,如果您在原始示例中点击“继续”,您将陷入无限循环,因为“fromRow”永远不会更改值。

Try:

var fromRow = document.getElementById("row_1");

while(fromRow !== null)
{
    var fRowId = fromRow.id;
    if (!fRowId || fromRow.nodeType != 1) {
        fromRow = fromRow.nextSibling;
        continue;
    }

    // THIS ONLY gets done once and alerts "row_1" ONLY :(
    alert(fRowId);
    fromRow = fromRow.nextSibling;
}

While fromRow.nextSibling != null would halt on the second to last iteration because you already set fromRow to its nextSibling at the end. Also, you don't necessarily want to halt if the next node isn't an element, you just want to move onto the next one if possible. Finally, if you hit the continue in your original example, you'll run into an endless loop because fromRow would never change value.

╄→承喏 2024-09-09 23:30:45

while 循环一旦遇到非类型 1 的节点就会停止。因此,如果元素之间有任何空格,while 循环将在第一个元素之后中断。

您可能想要的是:

while(fromRow.nextSibling != null)
{
    if(fromRow.nodeType == 1) {
        ...
    }
}

Your while loop stops as soon as it encounters a node not of type 1. So if there is any whitespace between your elements the while loop will break after the first element.

What you probably want is:

while(fromRow.nextSibling != null)
{
    if(fromRow.nodeType == 1) {
        ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文