开发者

Get child elements from a parent but not first and last

开发者 https://www.devze.com 2023-02-01 18:13 出处:网络
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?

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

精彩评论

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