When using querySelectorAll, is there a way to reference the immediate children of the context node, without using IDs?

柔情痞子 提交于 2019-11-29 13:56:09
Rob W

No, there isn't a way (yet) to reference all childs of some element without using a reference to that element. Because > is a child combinator, which represents a relationship between a parent and child element, a simple selector (a parent) is necessary (which is missing in you example).

In a comment, BoltClock said that the Selectors API Level 2 specification defines a method findAllname may change"which accepts as an argument what will probably be known as a relative selector (a selector that can start with a combinator rather than a compound selector)".
When this is implemented, it can be used as follows:

a_div.findAll('> div');
document.querySelector('#a').querySelectorAll(':scope > div')

I am not sure about browser support, but I use it on chrome and chrome mobile packaged apps and it works fine.

I think I must be misunderstanding your question, but this is how I'm interpreting it..

var a = document.getElementById('a')
var results = a.querySelectorAll('div');

results will hold all your child divs now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!