查询选择器中的“父”>“子”选择器

anj*_*esh 2 javascript selector

如何获得> p>在querySelector中工作?

<div id="container">
    <p>
        <a href="#">Link</a>
    </p>
</div>

<script type="text/javascript">    
console.log(document.getElementById("container").querySelector("> p > a"));
</script>
Run Code Online (Sandbox Code Playgroud)

语法错误:指定了无效或非法的字符串

Utk*_*nos 5

正是这种领先>阻止了你。如果您要使用,querySelector那么也可以通过这种方式获取容器,而不要使用getElementById

document.querySelector("#container > p > a")
Run Code Online (Sandbox Code Playgroud)

  • 或者只是等待`findAll`被实现。请参阅链接的 [重复问题](http://stackoverflow.com/questions/11440725/when-using-queryselectorall-is-there-a-way-to-reference-the-immediate-children)。 (2认同)