Get elements just 1 level below the current element by javascript

后端 未结 8 2155
自闭症患者
自闭症患者 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: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!'));
    }
    a
    aa
    ab
    aba
    b
    ba
    bb
    bba
    c
    ca
    cb
    cba

提交回复
热议问题