去年在 SF 的 DevDays 上(在 jQuery 1.4 发布之前),我认为他们提到了 1.4 中即将推出的功能,该功能允许您在链中引用当前的 jQuery 对象。我已经通读了 1.4 的所有改进,但找不到它。有谁知道如何做到这一点?
示例
在使用与当前对象相关的方法(如 .next())时,能够访问当前 jQuery 对象会很有帮助:
// 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])'));
最佳答案
我认为这是最接近的可用方法,使用 .andSelf()
:
var items = $(this).closest('tr').next(':not([id])').andSelf();
这将转到 .next()
调用,但随后将 tr
添加回其中。一切都在链发生的上下文中,因此无论是跳跃添加元素,还是存储原始引用,都与当前可用的最接近。这些是跳转的有用函数:http://api.jquery.com/category/traversing/miscellaneous-traversal/
John Resig posted this concept a while back ,但据我所知,没有任何更接近的方法将其纳入 jQuery 核心。不过,如果您愿意,您可以使用博客上发布的代码。
关于jquery - 引用链中当前的 jQuery 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2647597/