简体   繁体   中英

jQuery xml find nodes without a specific child

is it possible to find all nodes that don't have a specified child node?

for example:

(xml)

<item>
    <name>item 1</name>
    <admin>true</admin>
</item>

<item>
    <name>item 2</name>
    <admin>true</admin>
</item>

<item>
    <name>item 3</name>
    <parent>item 1</parent>
    <url></url>
    <admin>false</admin>
</item>

I want to pick out all nodes that don't have a child node "parent". I can do this if I set an attribute named parent and call:

(jquery)

$(xml).find("item:not([parent])").each

but I was wondering if this is possible by using a child node instead.

You may get other good suggestions -- possibly based only on selectors -- but I believe this will work.

$('item').filter(function() {
    return $(this).find('parent').length === 0;
}).doSomethingWithTheSetOfItemsWithoutParents();

UPDATE

Based on selector documentation , I think this one will do what you want:

$('item:not(:has(parent))')

您可以使用jQuery的本机parent()选择器吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM