Get elements just 1 level below the current element by javascript

后端 未结 8 2007
自闭症患者
自闭症患者 2021-02-05 20:30

I need to access the DOM tree and get the elements just 1 level below the current element.

Read the following code:

相关标签:
8条回答
  • 2021-02-05 21:03

    This is simplier than you think:

    var nodes = node.querySelector("node > div");
    
    0 讨论(0)
  • 2021-02-05 21:05

    Universal selectors can do the trick:

    var subNodes = document.querySelectorAll("#node > *");
    

    Query parts:

    #node is unique container selector

    > next slector should be applied only on childs

    * universal selector that match every tag but not text

    Can I use universal selector

    0 讨论(0)
  • 2021-02-05 21:05

    I think node.childNodes is the right place to start. You could (to make it work with FF too), test the nodeName (and possibly nodeType) of all child nodes you get, to skip text nodes.

    Also you might have a look at some javascript library like prototype, which provide a lot of useful functions.

    0 讨论(0)
  • 2021-02-05 21:11

    I've added some text so we can see that it is working, and JavaScript that will add "added!" to the bottom of each of the divs at the base:

    var cDiv = document.querySelectorAll('body > div > div'), i;
    for (i = 0; i < cDiv.length; i++)
    {
    	cDiv[i].appendChild(document.createTextNode('added!'));
    }
    <div id="node">
        <div id="a">a
            <div id="aa">aa
                <div id="ab">ab
                    <div id="aba">aba</div>
                </div>
            </div>
        </div>
        <div id="b">b
            <div id="ba">ba
                <div id="bb">bb
                    <div id="bba">bba</div>
                </div>
            </div>
        </div>
        <div id="c">c
            <div id="ca">ca
                <div id="cb">cb
                    <div id="cba">cba</div>
                </div>
            </div>
        </div>
    </div>

    0 讨论(0)
  • Try this (late answer, but can be useful for others):

    var list;
    list=document.getElementById("node").querySelectorAll("#node>div");
    
    0 讨论(0)
  • 2021-02-05 21:22

    In my opinion the easiest way to do this is to add a class name to the first level child nodes:

    <div id="node">
        <div id="a" class="level_1">
            <div id="aa">
                <div id="ab">
                    <div id="aba"></div>
                </div>
            </div>
        </div>
        <div id="b" class="level_1">
            <div id="ba">
                <div id="bb">
                    <div id="bba"></div>
                </div>
            </div>
        </div>
        <div id="c" class="level_1">
            <div id="ca">
                <div id="cb">
                    <div id="cba"></div>
                </div>
            </div>
        </div>
    </div>
    

    and then to use the method getElementsByClassName, so in this case:

    document.getElementById('node').getElementsByClassName('level_1');
    
    0 讨论(0)
提交回复
热议问题