ancestor-or-self: $(...).parents().andSelf()
descendant-or-self: $(...).find('*').andSelf();
These return the exact same collection as it's XPath counterparts.
I'm not familiar with XPath, I'm intrigued by the uses for 'preceding'
and 'following', do you have an example case? It doesn't make much
sense to me in the context of a well structured document. Anyway, it
turned out to be pretty easy (not sure it's 100% correct):
$.fn.following = function(){
return this.nextAll().add(this.parents().nextAll()).filter(':not
(script)');
}
$.fn.preceding = function(){
return this.prevAll().add(this.parents().prevAll()).filter(':not
(script)');
}
(I added a filter for script tags 'cause generally you're not going to
manipulate them together with visible elements..)
cheers,
- ricardo
On 28 nov, 12:34, Mark Gibson <[EMAIL PROTECTED]> wrote:
> For people who are familiar with XPath, I think it would be useful to
> list
> the similarities between XPath axes and jQuery selection/traversal
> methods,
> and also add any useful discrepancies into jQuery, or describe how to
> emulate
> them.
>
> This is my interpretation, additions would be most welcome, ideally
> I'd like
> to cover all the appropriate axes (not marked n/a).
>
> XPath Axis jQuery
>
> ancestor context.parents(...)
> ancestor-or-self ?
> attribute n/a
> child context.children(...) or "context > ..." or $
> ("> ...", context)
> descendant context.find(...) or "context ..." or $(...,
> context)
> descendant-or-self ?
> following ?
> following-sibling context.nextAll(...) or "context ~ ..."?
> namespace n/a
> parent context.parent(...)
> preceding ?
> preceding-sibling context.prevAll(...) or "... + context"?
> self context.filter(...) ???
>
> I've found a few situations where I've needed 'ancestor-or-self',
> 'following', and
> 'preceding' style selection, but not found any easy way to do it.
>
> For example, keyboard navigation of a tree (key up & down) would
> benefit
> greatly from the following and preceding axes.
>
> Finding descendants and including all the elements specified in an
> aria-owns
> attribute in the search would benefit from the descendant-or-self
> axis.
> (ie. find in the context element, and find-inc-self for all elements
> specified
> by the attribute)
>
> I know there is a .andSelf(), but I don't think it really cover these
> situations.
> Previously I've suggested a .selfOrParents() method, but I think a
> general
> 'include self' switch before a traversal method would be better:
>
> context.incSelf().find(...)
> context.incSelf().parents(...)
>
> Can the jQuery team see this as being generally useful, and accepted
> into the core?
> Or any alternative suggestions?
>
> Also, any suggestions on how to do following or preceding style
> selection?
>
> Cheers
> -Mark
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---