Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 访问使用.each()循环的元素,而不调用$(this)_Jquery - Fatal编程技术网

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

Jquery 访问使用.each()循环的元素,而不调用$(this),jquery,Jquery,我正在使用.each()迭代jQuery对象$firstparation中的链接。当我迭代这些链接时,我希望以绝对的方式引用它们,而不调用$(this),每次调用它时都会创建一个新对象。因为当我在链接上独立迭代两次时(如下面的代码所示),两个实例中完成的工作就不会合并 $firstParagraph = $("p").eq(0); // Iterate over the links once $firstParagraph.find("a").each( function() {

我正在使用
.each()
迭代jQuery对象
$firstparation
中的链接。当我迭代这些链接时,我希望以绝对的方式引用它们,而不调用
$(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
    });

您可以这样做:

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

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

我不确定这是否是个好主意,因为两个循环之间可能会发生变化。

您可以这样做:

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

// Iterate over the links a second time
$firstParagraph.find("a").each( function(i)
    {
        $this = elemArray[i];
        // 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)
概念中,这样您就可以将其取回。(Docs.)

不要像你所要求的那样粗暴地违背jQuery的粒度,让我来建议你如何做你真正需要做的事情:使用
$(this).data('somekey',someval)
$(this).data('somekey')
,而不是直接将
$(this)
作为一个对象修改,从而丢失你试图做的任何事情,将数据附加到您的
$(this)
概念中,这样您就可以将其取回。(文档。)

为什么不附加到数组?当您使用简单变量时,它将始终覆盖它。

为什么不附加到数组中?当您使用简单变量时,它将始终覆盖它。

您不应该修改
$(此)
。你想做什么?为什么不把这两种逻辑结合在一个循环中呢?海报并不是要修改这个对象>>对于任何有意义地回答这个问题的人来说,你真的需要告诉我们你在这两个循环中做了什么。如果您所做的只是简单地创建jQuery实例,而不需要使用您试图避免创建的jQuery包装器(例如获取/设置锚的
id
href
,等等),那么您可能根本不需要创建jQuery实例。您不应该修改
$(此)
。你想做什么?为什么不把这两种逻辑结合在一个循环中呢?海报并不是要修改这个对象>>对于任何有意义地回答这个问题的人来说,你真的需要告诉我们你在这两个循环中做了什么。您可能根本不需要创建jQuery实例,如果您所做的一切都是在不使用您试图避免创建的jQuery包装器的情况下轻松完成的事情(例如获取/设置锚的
id
href
,等等)。这听起来很有希望。谢谢!这听起来很有希望。谢谢!