jQuery xml find nodes without a specific child
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?
精彩评论