How do I use jQuery to select all children except a select element

后端 未结 7 828
甜味超标
甜味超标 2020-12-07 18:43

I have a div (let\'s say the id is \"container\") with many elements in it, including a select element. I\'d like to select all everything in the div except the select. Thin

7条回答
  •  囚心锁ツ
    2020-12-07 19:11

    You can simply omit the wildcard as it is optional in this case, but keep the space:

    $('#container :not(select)');
    

    Alternatively, use the .not() method to filter out the select after selecting all children:

    $('#container').children().not('select');
    

    If your problem is that children of select are still included, you could explicitly filter those out with the .not() method:

    $('#container').children().not('select, option, optgroup');
    

    or select direct children only:

    $('#container > :not(select)');
    

    You can try out jQuery selectors at the interactive jQuery selector tester for quick feedback; try for example div :not(ol) or div > :not(ol) to get a feel for what difference the direct child (>) selector makes.

提交回复
热议问题