Giter Club home page Giter Club logo

Comments (2)

chitosai avatar chitosai commented on August 11, 2024

来自3年后的回复,抱歉回复晚了一(hen)点(jiu)😅

今天终于是看了下文档,但我们是需要递归的,因为父元素有时会决定子元素是否需要继续遍历,或者是遍历的时候是否需要做特殊处理。所以好像没办法发挥TreeWalker的优势?

from eye_protector.

mozbugbox avatar mozbugbox commented on August 11, 2024

可以考虑不直接使用 nextNode(), 而是通过使用 firstChild/nextSibling/parentNodenextSibling() 的组合手动进行 deep first 的遍历,应该可以把父元素不断的 push/pop 到一个数组里。这样就可以访问父祖元素了。

类似:

parents = []
node = root
goUp = false
while (true) {
    if (goUp) {
        goUp = false
        node = walker.parentNode()
        if (!node) {
            break;
        } else {
            parents.pop()
            node = walker.nextSibling()
            if (!node) {
                goUp = true 
                continue
            }
        }
    } else {
        node = walker.firstChild()
        if (!node) {
            node = walker.nextSibling()
            if (!node) {
                goUp = true;
                continue
            } 
        } else { 
            parents.push(node.parentNode)
        }
    }
}

from eye_protector.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.