我可以使用以下内容来获取元素:
document.getElementById('the-container');
有没有办法获得此组件的子组件(不一定是直接子组件)。
<div id="the-container">
<div class="whatever">
<form>
.....
我想获取表单元素,而不添加 id/class 或通过 form
标记定位它(可能是页面上的其他表单)。
没有 jquery,但对 HTML5 很满意。
最佳答案
querySelector
是 Element 对象的属性,并采用选择器作为其第一个参数。
document.getElementById('the-container').querySelector("form");
您还可以通过更精细的选择器直接在整个文档上使用它:
document.querySelector('#the-container form')
关于javascript - 使用选择器获取子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43120676/