Get elements just 1 level below the current element by javascript

后端 未结 8 2009
自闭症患者
自闭症患者 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:27

    You could use a function that rules out all non-element nodes:

    function getChildNodes(node) {
        var children = new Array();
        for(var child in node.childNodes) {
            if(node.childNodes[child].nodeType == 1) {
                children.push(child);
            }
        }
        return children;
    }
    

提交回复
热议问题