开发者

jQuery xml find nodes without a specific child

开发者 https://www.devze.com 2022-12-19 18:09 出处:网络
is it possible to find all nodes th开发者_如何学Pythonat don\'t have a specified child node? for example:

is it possible to find all nodes th开发者_如何学Pythonat 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))')


Could you use jQuery's native parent() selector?

0

精彩评论

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

关注公众号