Referencing the current jQuery object in a chain?
At DevDays in SF last year (before jQuery 1.4 was released), I thought they mentioned an upcoming feature in 1.4 that would allow you to reference the current jQuery object while in a chain. I've read through all of the 1.4 improvements and wasn't able to find it. Does anyone know how this can be done?
Example
Being able to access the current jQuery object would be helpful when working with methods that are in relation to the current object like .next():
// Current way
var items = $(this).closest('tr');
items = items.add(items.next(':not([id])'));
// Magical 1.4 way? Is there a "chain"-like object?
var items = $(this).closest('tr')
.add(chain.next(':not([id])')开发者_运维问答);
I think this is the closest available, using .andSelf()
:
var items = $(this).closest('tr').next(':not([id])').andSelf();
This goes to the .next()
call, but then adds the tr
back to it. Everything is in the context of where the chain occurs, so either jumping around to add elements, or storing the original reference as you have it is as close as is currently available. These are the helpful functions for jumping around: http://api.jquery.com/category/traversing/miscellaneous-traversal/
John Resig posted this concept a while back, but to my knowledge, nothing closer to that has made it into jQuery core. You can however use the code posted on the blog there if you want.
Possibly andSelf()
http://api.jquery.com/andSelf/
This returns the previous selection, but you would then have to use :not([id]) on everything..
精彩评论