查询DOM时跳过容器中的元素

查询DOM时跳过容器中的元素,dom,jquery-selectors,selectors-api,Dom,Jquery Selectors,Selectors Api,当使用或任何DOM查询方法时,我希望跳过给定子容器中的元素进行查询 示例: <div id="container-one"> <div> <input type="text" name="first"> <input type="text" name="second"> <!-- skip below --> <div id="container-two">

当使用或任何DOM查询方法时,我希望跳过给定子容器中的元素进行查询

示例:

<div id="container-one">
    <div>
      <input type="text" name="first">
      <input type="text" name="second">

      <!-- skip below -->
      <div id="container-two">
        <input type="text" name="third">
        <input type="text" name="fourth">       
      </div>
    <div>
</div>

假设在上述情况下,如果从
#container one
元素进行查询,则要跳过
#container two
中的元素。因此,对
#container one
的查询应该只返回
[第一、第二]
元素,并跳过其他元素(第三、第四)

感谢您的宝贵意见。

尝试以下方法:

document.querySelectorAll('div.container-one')[0].querySelectorAll(':scope > input')
更新(您不能为此使用querySelectorAll):

inputs=document.getElementsByTagName(“输入”);
input_two=document.getElementById('container-two').getElementsByTagName('input');
var输入_cont_two_name_数组=[];

对于(j=0;jInput元素可以在层次结构中的任何位置,而不仅仅是直接子元素。因此,
':scope>input'
在这里不起作用。根据提供的信息,我为孩子们制作了它。如果你能给我一个例子,我们可以看到可以做什么。即使它不是直接子元素,它也会为你提供输入元素…你可以使用jsfiddle到c检查登录控制台。我已经更新了我问题中的html,但您提供的解决方案不起作用。谢谢,但更新后的解决方案不是通用解决方案。
inputs = document.getElementsByTagName("input");
input_two = document.getElementById('container-two').getElementsByTagName('input');

var input_cont_two_name_array = [];
for(j=0;j<input_two.length;j++){
    input_cont_two_name_array.push(input_two[j].name);
}

for(i=0;i<inputs.length;i++){
    input_name_id = inputs[i].name;
    if(input_cont_two_name_array.indexOf(input_name_id) == -1){
      //do your stuff
    }
}