1

私は次のようなHTML構造を持っています:

<tr class="v65-productDisplay-row"></tr>
<tr class="v65-productDisplay-row PriceAndButton"></tr>
<tr class="v65-productDisplay-row PhotoLine"></tr>
<tr class="v65-productDisplay-row"></tr>
<tr></tr>
<tr class="v65-productDisplay-row"></tr>
<tr class="v65-productDisplay-row PriceAndButton"></tr>
<tr class="v65-productDisplay-row PhotoLine"></tr>
<tr class="v65-productDisplay-row"></tr>
<tr></tr>
<tr class="v65-productDisplay-row"></tr>
<tr class="v65-productDisplay-row PriceAndButton"></tr>
<tr class="v65-productDisplay-row PhotoLine"></tr>
<tr class="v65-productDisplay-row"></tr>
<tr></tr>
...and so on

各 PriceAndButton TR をターゲットにして、対応する各 PhotoLine TR の後に挿入しようとしています。ここに私がこれまでに持っているコードがありますが、まだ機能していません:

        jQuery(".PriceAndButton").each(function() {
            var IndexValue = $(this).index();
            $(this).insertAfter(".PhotoLine:eq(" + IndexValue + 1 + "");
        });

私の考えでは、各 PriceAndButton 要素のインデックス値を取得し、その eq がインデックス値 + 1 に等しい PhotoLine TR の後に挿入することでした。PhotoLine TR は常に直後にあるため、これは正しく機能するはずですか?

4

2 に答える 2

1

あなたのコード(閉じ括弧を修正しました):

".PhotoLine:eq(" + IndexValue + 1 + ")";

文字列として連結されます。が 3 の場合、代わりにIndexValue文字列を取得します。あなたが意味したのは:.PhotoLine:eq(31).PhotoLine:eq(4)

".PhotoLine:eq(" + (IndexValue + 1) + ")";

しかし、それでもうまくいくとは限りません。

簡単にするために、eq代わりに を選択することで、使用をまったく避けることができます.next('.PhotoLine')(または単に.next()、ただし、予期しない驚きを避けるために正確にするのが好きです):

$('.PriceAndButton').each(function(i,el) {
    $(this).insertAfter($(this).next('.PhotoLine'));
});

http://jsfiddle.net/mblase75/YWaYN/

于 2013-04-02T18:21:11.687 に答える
1

次のように jQueryの next メソッドを使用できます。

$(".PriceAndButton").each(function() {
    var self = $(this);
    self.insertAfter(self.next());
});
于 2013-04-02T18:21:43.823 に答える