Actually, I just noticed it's missing the current element :-\
var selector =
element.ancestors().pluck("nodeName").invoke("toLowerCase").join("
> ");
selector += " > " + element.nodeName; // yup, that's ugly :)
I don't really see how many times you'd need an "exact" selector like
this one to get all nodes with the same "ancestorship" (is that even a
word?), so I don't know how useful that'd be as an Element method.
What would might be a little more useful (but again, not that much, so
I don't know) is something that gets all the children at a specified
level, ignorant of who the intermidiate nodes are.
So on the OP's example, something that would collect all the <b> tags
by calling $(document.body).childrenAt(3);
Element.addMethods({
childrenAt: function(element, level) {
if (level < 1)
return element.previousSiblings().concat(element,
element.nextSiblings());
return element.childElements().map(function(e) {
return e.childrenAt(level - 1);
}).flatten().uniq();
}
});
Something like that might do it (haven't tested it much), but it's
gonna be slow like hell on large trees.
Best,
-Nicolas
On Jan 11, 2008 8:13 PM, Justin Perkins <[EMAIL PROTECTED]> wrote:
>
> > var selector =
> > element.ancestors().pluck("nodeName").invoke("toLowerCase").join("
> > > ");
>
> Quite a handy little piece of code there Nicolas, with a slight
> modification to append the element ID and that'd be a great Element
> utility method.
>
> -justin
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" 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/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---