2

配列をループしてから、リストをループしようとしています。

私が抱えている問題は<li>、配列全体が追加されるたびに発生することですが、必要なのは、配列のインデックス(0)が最初のliに追加され、index(1)が2番目のliに追加されるなどです。

コード:

// Create our test array.
var arrValues = [ "one", "two", "three" ];

// Loop over each value in the array.
$.each(arrValues,function( intIndex, objValue ){
    $("#list span").each(function() {
        $(this).append(objValue);
    });
});

現在の出力:

<ul id="list">
        <li>Content 1 here<span>onetwothree</span></li>
        <li>Content 2 here<span>onetwothree</span></li>
        <li>Content 3 here<span>onetwothree</span></li>
    </ul>

必要な出力:

<ul id="list">
        <li>Content 1 here<span>one</span></li>
        <li>Content 2 here<span>two</span></li>
        <li>Content 3 here<span>three</span></li>
    </ul>

どんな助けにも感謝します:)

4

2 に答える 2

1

これは実装するのがより簡単な方法だと思います:

    $("#list span").each(function( intIndex, objValue ){
        $(this).append(arrValues[intIndex]);
    });

あなたが現在抱えている問題は、配列を反復処理しており (3 回の反復)、各反復が<span>s の数全体をループしているため、合計 9 回の反復であることです。

于 2013-02-11T16:10:32.727 に答える