递归过多

发布于 2024-11-08 20:45:29 字数 553 浏览 0 评论 0 原文

我在 firefox 4 上使用 JS,并收到以下代码的“太多递归错误”:

  extractText: function(domObj) {
    if (domObj == null) {
        return "";
    } else {
        var acc = "";
        if (domObj.nodeType == Node.TEXT_NODE) {
            acc += domObj.nodeValue;
        }
        if (domObj.hasChildNodes()) {   
            var children = currentObj.childNodes;
            for (var i = 0; i < children.length; i++) {
                acc += sui.extractText(children[i]);
            }
        }
        return acc;
    }
  }
};

有人吗?

I'm using JS on firefox 4 and get the "too much recursion error" for the following code:

  extractText: function(domObj) {
    if (domObj == null) {
        return "";
    } else {
        var acc = "";
        if (domObj.nodeType == Node.TEXT_NODE) {
            acc += domObj.nodeValue;
        }
        if (domObj.hasChildNodes()) {   
            var children = currentObj.childNodes;
            for (var i = 0; i < children.length; i++) {
                acc += sui.extractText(children[i]);
            }
        }
        return acc;
    }
  }
};

Anyone?

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

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

发布评论

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

评论(2

七色彩虹 2024-11-15 20:45:29

我认为这一行:

        var children = currentObj.childNodes;

应该是:

        var children = domObj.childNodes;

在我看来,您对“currentObj”的引用似乎是从顶部开始,而不是从正在检查的元素下降。当然很难说,因为您没有包含“currentObj”的相关定义或初始化。

I think that this line:

        var children = currentObj.childNodes;

should be:

        var children = domObj.childNodes;

It looks to me as if your reference to "currentObj" is starting over at the top instead of descending from the element under examination. It's hard to tell of course because you didn't include the relevant definition or initialization of "currentObj".

半衾梦 2024-11-15 20:45:29

您还可以尝试迭代方法而不是递归:

extractText: function(domObj) {
    if (!(domObj instanceof Node)) return null;
    var stack = [domObj], node, tf = [];
    while (stack.length > 0) {
         node = stack.pop();
         switch (node.nodeType) {
         case Node.TEXT_NODE:
             tf.push(node.nodeValue);
             break;
         case Node.ELEMENT_NODE:
             for (var i=node.childNodes.length-1; i>=0; i--)
                 stack.push(node.childNodes[i]);
             break;
         }
    }
    return tf.join("");
}

该算法使用以下方法实现深度优先搜索仍然必须访问的节点的堆栈。如果堆栈上的第一项是 节点实例。然后对于堆栈上的每个节点:如果它是 Text节点,其值被添加到文本片段数组tf中;如果它是一个 元素节点,其子节点以相反的顺序放入堆栈,以便第一个子节点位于堆栈顶部。重复这些步骤直到堆栈为空。最后,使用数组的 join 方法将 tf 中的文本片段组合在一起。

You can also try an iterative approach instead of recursion:

extractText: function(domObj) {
    if (!(domObj instanceof Node)) return null;
    var stack = [domObj], node, tf = [];
    while (stack.length > 0) {
         node = stack.pop();
         switch (node.nodeType) {
         case Node.TEXT_NODE:
             tf.push(node.nodeValue);
             break;
         case Node.ELEMENT_NODE:
             for (var i=node.childNodes.length-1; i>=0; i--)
                 stack.push(node.childNodes[i]);
             break;
         }
    }
    return tf.join("");
}

This algorithm implements a depth first search using a stack for the nodes that still must be visited. The first item on the stack is domObj if it’s a Node instance. Then for each node on the stack: if it is a Text node, its value is added to the text fragments array tf; if it’s an Element node, its child nodes are put on the stack in reverse order so that the first child is on top of the stack. These steps are repeated until the stack is empty. At the end, the text fragments in tf are put together using the array’s join method.

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