我使用 .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/