On Nov 30, 7:05 pm, ricardobeat <[EMAIL PROTECTED]> wrote:
> ancestor-or-self: $(...).parents().andSelf()
The only trouble with .andSelf(), is that it tacks the previous set to
the end of the current set,
and is no use with positional filtering.
Example, I already have a jQuery set to search from ('context'), and I
want to find the first <ul>
in the ancestor-or-self axis, then .andSelf() is no use. At present I
have to do something like:
$(context).filter('ul:first').add($(context).parents('ul:first')).eq
(0);
Quite inefficient.
> descendant-or-self: $(...).find('*').andSelf();
This is ok if you want everything in any order, I supposed you could
just tack on a .filter() for simple
filters, but :first would again not work as expected and it seems
inefficient, when all we need to do
is modify find() to include the context set.
> 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 think you've hit the nail on the head with this, it seems so obvious
now!
I'll test it out.
The best use case is for keyboard navigation, when you hit the down
key, you'd
normally want to focus the next item down the document regardless of
structure.
ie. in a tree, down proceeds to the next item in the document whether
it is a sibling or an aunt, or your granddads second cousin removed ;)
Cheers Ricardo
- Mark
> (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
-~----------~----~----~----~------~----~------~--~---