0

私は次のhtmlを持っています

<div class="row-updated">
    <div>
        <span>Title</span>
        <span>Some text here</span>
    </div>
</div>

<div class="row">
    <div>
        <span>Title</span>
        <span>Some text here</span>
    </div>
</div>

<div class="row">
    <div>
        <span>Title</span>
        <span>Some text here</span>
    </div>
</div>

<div class="row-updated">
    <div>
        <span>Title</span>
        <span>Some text here</span>
    </div>
</div>

クラス「row-updated」を使用して、各 div の 2 番目のスパンのテキストを更新したいと考えています。

var updated = $('.row-updated span:eq(1)');

$.each( updated, function( key, value ) {
    $(this).text('New text here');
});

しかし、これはすべての行を更新していないようです。最初は更新されますが、2 番目は更新されません。

4

4 に答える 4

3

.eq()あなたが思っていることをしません。ここを使用する必要があり.nth-child()ます。

// nth-child() is 1-indexed as @ᾠῗᵲᄐᶌ  pointed out in the comment (thanks!).
var updated = $('.row-updated span:nth-child(2)');

$.each( updated, function( key, value ) {
    $(this).text('New text here');
});

.eqnth ではなく、結果セット全体nthから要素を返します。span

于 2013-07-15T14:35:44.510 に答える