jQuery 转移到另一个 DOM 节点后获取原始 $(this) ?

标签 jquery

我有以下 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/

相关文章:

javascript - 如何轻松地将内部 html 应用于多个元素?

javascript - 使用 AJAX 传递包含 `&` 的参数

javascript - 单击提交按钮时的 HTML 表单验证

php - 使用ajax功能时重新加载页面

javascript - 无法调整文本区域的高度

javascript - 加载 <script> 内容后是否加载 jQuery.load() ?

.net - 使用 Jquery Mobile 使用 WCF 服务返回 400 错误请求

javascript - 从表创建 jquery 实时预览

jquery - 在每次悬停时运行 jQuery slideDown

javascript - 使用 jQuery 在 HTML 中显示 var