代码之家  ›  专栏  ›  技术社区  ›  Klyner

如何选择不在具有特定ID的元素中的所有元素?

  •  1
  • Klyner  · 技术社区  · 6 年前

    例如,是否可以选择不包含在具有ID的元素中的所有div myId ?

    这个 not() 选择器可能有实现这一点的功能,但我不知道如何实现。

    例如:

    <div>
       <div id="myId">
          <input/>
          <input/>
          <input/>
       </div>
       <input>I only want to select this one</input>
    </div>
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Pete Irfan TahirKheli    6 年前

    你可以使用 .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>
        2
  •  0
  •   peeebeee    6 年前

    你的问题有点令人困惑,因为你说你想选择 div 但您的示例指示您要选择 input .假设是 输入 你想要,

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

    应该这样做。