admin管理员组

文章数量:1299983

I would like to know how could I write a jQuery selector that get all children from a parent element except first and last child?

Example of my current HTML:

<div id="parent">
    <div>first child( i don't want to get)</div>
    <div>another child</div>
    <div>another child</div>
    <div>another child</div>
    (...)
    <div>another child</div>
    <div>another child</div>
    <div>last child (i dont want to get neither)</div>
</div>

I would like to know how could I write a jQuery selector that get all children from a parent element except first and last child?

Example of my current HTML:

<div id="parent">
    <div>first child( i don't want to get)</div>
    <div>another child</div>
    <div>another child</div>
    <div>another child</div>
    (...)
    <div>another child</div>
    <div>another child</div>
    <div>last child (i dont want to get neither)</div>
</div>
Share Improve this question edited Dec 28, 2010 at 17:25 DVK 130k33 gold badges218 silver badges334 bronze badges asked Dec 28, 2010 at 17:24 CleitonCleiton 18.2k13 gold badges47 silver badges59 bronze badges 3
  • 2 It's a good idea to spell check your questions so they look more professional and not like you are a slacker who doesn't care. – DVK Commented Dec 28, 2010 at 17:26
  • DVK, I'm sorry, but I don't speak english as first language, I will try to be more carefull. – Cleiton Commented Dec 28, 2010 at 17:28
  • most modern web browsers have built-in spell checkers. I never pick on people's English, only on really obvious typos (as non-English-native-speaker, it's very easy for me to know which one's which :) – DVK Commented Dec 28, 2010 at 17:36
Add a ment  | 

6 Answers 6

Reset to default 11

Like this:

$('#parent > div:not(:first, :last)');

You can do this:

$('#parent div').not(':first').not(':last')

Or

$('#parent').children().not(':first').not(':last')

Here not method will filter out first and last elements from the selector.

More Information:

  • http://api.jquery./not/
$('#parent').children(':not(:first):not(:last)')
$('#parent').children().not(':first').not(':last')

Should work

$("#parent > div:not(:first, :last)");

Try this:

$(
            function()
            {
                var a = $("div#parent *:not(:first-child)").not(":last-child");
                alert(a.length);
            }
)

本文标签: javascriptGet child elements from a parent but not first and lastStack Overflow