2011-07-25 11 views
1

.each()を使用して、jQueryオブジェクト$firstParagraphのリンクを反復処理しています。リンクを繰り返していくうちに、呼び出すたびに新しいオブジェクトを作成する$(this)を呼び出すことなく、絶対の方法でそれらを参照したいと思います。リンク上でリンクを独立して2回反復すると(下のコードを参照)、両方のインスタンスで行われた作業が結合されないためです。これは物事が二つのループの間で変更することができますように良いアイデアがあれば

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 
    }); 

私はよく分からない:

$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 
    }); 
+1

あなたは '$(this)'を変更すべきではありません。あなたは何をしようとしているのですか? – SLaks

+0

あなたは何を改造していますか? – Sparkup

+2

両方のロジックを1つのループで組み合わせるのはなぜですか? – ShankarSangoli

答えて

4

、私はあなたが実際に何をする必要があるか実行する方法をアドバイスしてみましょう。代わりに、直接オブジェクトとして$(this)を変更するので、失うのに$(this).data('somekey', someval)$(this).data('somekey')を使用何をしようとしていても、あなたはそれを取り戻すことができるように、$(this)のコンセプトにデータを添付してください。 (文書here

+0

これは非常に有望ですね。どうもありがとう! – Randomblue

+0

@Randomblue:問題ありません。 :) – chaos

0

あなたはこのような何かを行うことができます。代わりに、あなたが求めているようにjQueryの穀物に対して激しく行くの

1

なぜあなたは配列に追加しませんか?単純な変数を使用しているので、常にそれを上書きします。

関連する問題