开发者

Get child elements from a parent but not first and last

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 c开发者_StackOverflowhild</div>
    <div>another child</div>
    (...)
    <div>another child</div>
    <div>another child</div>
    <div>last child (i dont want to get neither)</div>
</div>


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.com/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);
            }
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜