jquery - 访问使用 .each() 循环的元素而不调用 $(this)

标签 jquery

我使用 .each() 来迭代 jQuery 对象 $firstParagraph 中的链接。当我迭代链接时,我想以绝对方式引用它们,而不是调用每次调用时都会创建一个新对象的$(this)。因为当我在链接上独立迭代两次时(如下面的代码所示),那么在两个实例中完成的工作不会合并。

$firstParagraph = $("p").eq(0);

// Iterate over the links once
$firstParagraph.find("a").each( function()
    {
        $this = $(this);
        // Modify $this
    });

// Iterate over the links a second time
$firstParagraph.find("a").each( function()
    {
        $this = $(this);
        // I want to modify $this again, but without loosing
        // the work I did in the first iteration
    });

最佳答案

不要像你问的那样强烈反对 jQuery 的本质,让我建议你如何做你真正需要做的事情:使用 $(this).data('somekey', someval)$(this).data('somekey') ,而不是直接将 $(this) 修改为对象,从而丢失您尝试的任何内容为此,请将数据附加到您的 $(this) 概念中,以便您能够将其取回。 (文档 here 。)

关于jquery - 访问使用 .each() 循环的元素而不调用 $(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819917/

相关文章:

javascript - 将 jQuery 单击事件添加到 Kendo UI 网格列模板中的按钮

javascript - 单击提交时禁用页面刷新并转到下一个选项卡

javascript - 如果元素没有类,则不显示;否则,显示?

javascript - 在 div 中使用 onclick 函数显示/隐藏另一个 div

javascript - 在 JavaScript 中部分复制对象数组的最优雅的方法是什么

javascript - 使用 Java Servlet 的 SweetAlert

jquery - 如何定位 jQuery 对话框?

javascript - 将点击事件绑定(bind)到点击

javascript - 如何彻底清理 select2 控件?

jquery - 动态容器高度(仅限 jQuery)