我需要找到某个类,然后找到一个在其下面嵌套了多个元素的输入。我需要目标类名称来附加它。
我以为我可以使用 .queryselector 但似乎不行。我无法在输入中使用 ID。
var input = document.querySelectorAll('.pt-page.pt-subpage-current input');
var solve = input[0];
console.log(solve);
<div class="pt-page pt-subpage-current"> /*only identifier*/
<div class="container">
<div class="question">
<div class="input">
<input name="school" type="text" id="schoolCode1" autocomplete="off">/*target*/
</div>
</div>
</div>
</div>
我以为我可以像上面一样使用查询选择器(我已经经历了多次迭代),但没有骰子。
最佳答案
Use the direct descendant selector >
$('#mainDiv > p:first')
or even children()
$('#mainDiv').children('p').first()
关于javascript - 如何找到非间接/嵌套的父级的子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55869709/