Ok, so here's what 开发者_如何学编程I've got:
I have a list of elements cached in a variable:
elementList = $(".list-of-elements-with-this-class");
I also have a dynamically generated element from that list cached in another variable:
elementList.click(
function()
{
cachedItem = $(this);
}
);
What I want to do is locate cachedItem in elementList and then select cachedItem's previous or next sibling in the list.
So pseudo code would look like this:
nextCachedItem = elementList.find(cachedItem).next();
or
prevCachedItem = elementList.find(cachedItem).prev();
Obviously, the above doesn't work. :-)
Thanks for your help in advance!
-Tim.
I believe next()
and prev()
deal with the DOM Element, not the jQuery nodelist returned by a query. Therefore you have to deal with the indexes of the nodelist in the jQuery object manually.
Try:
// RAW DOM Nodes
elementList.get(elementList.index(cachedItem) - 1); // previous
elementList.get(elementList.index(cachedItem) + 1); // next
OR:
// jQuery Objects/Node list
elementList.eq(elementList.index(cachedItem) - 1); // previous
elementList.eq(elementList.index(cachedItem) + 1); // next
精彩评论