如何 select 所有不在具有特定 id 的元素中的元素?

How to select all elements that are not in an element with a particular id?

是否可以 select 例如所有不包含在具有 id myId 的元素中的 div?

not()select或可能具有完成此功能的功能,但我不知道如何实现。

例如:

<div>
   <div id="myId">
      <input/>
      <input/>
      <input/>
   </div>
   <input>I only want to select this one</input>
</div>

您的问题有点令人困惑,因为您说您想要 select div,但是您的示例表明您想要 select 和 input。假设这是你想要的input

$(input:not(#myID > input))

应该这样做。

您可以使用 .not() 过滤掉不需要的输入:

$('input').not('#myId input').css({ // if inputs are always direct child, you can use #myId>input
  'border-color': 'red'
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div id="myId">
    <input type="text">
    <input type="text">
    <input type="text">
  </div>
  <input type="text">
</div>