我有以下 jQuery:
$(this).siblings().removeClass('active');
$(this).addClass('active');
是否可以将其合并为一行?你不能这样做:
$(this).siblings().removeClass('active').addClass('active');
因为选定的 DOM 节点现在是所有同级节点,而不是原始节点,因为您从原始选择中转移(sp?)了。
我想知道是否有一些选择器可以再次选择您的原始选择,您可以将其链接到第一行,以便所有内容都在一行中。
最佳答案
我相信您正在寻找的是 the .end()
function 。引用该文档:
Most of jQuery's DOM traversal methods operate on a jQuery object instance and produce a new one, matching a different set of DOM elements. When this happens, it is as if the new set of elements is pushed onto a stack that is maintained inside the object. Each successive filtering method pushes a new element set onto the stack. If we need an older element set, we can use end() to pop the sets back off of the stack.
关于jQuery 转移到另一个 DOM 节点后获取原始 $(this) ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18689650/