1

要素を繰り返し処理したいと思います。各line要素には、子要素としてitem要素があります。したがって、最初に行を繰り返してから、特定のアイテム要素、たとえば3番目の要素に対処したいと思います。これは私が持っているコードです:

$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
    currentLine.(".item").each(function(k) {
        $(".pricetable .righttable .line .item").eq(i).addClass("active");
    });
});

問題は、 itemクラスで子要素をアドレス指定する方法がわからないことです。

currentLine.(".item").each(function(k)

これは動作しません。

XMLフィルターは非XML値({0:#2 =({})、length:1、prevObject:{length:3、prevObject:{0:#1 =({})、context:#1#に適用されます、length:1}、context:#1#、selector:"。pricetable.righttable .line:not(.blank)"、0:#2#、1:({})、2:({})}、 context:#1#、selector:"。pricetable.righttable .line:not(.blank).slice(0,1)"})file:/// C:/Users/xxx/lib/pricetable.js行112

編集:
うわー、私は私がこんなに良くて速い反応を得るとは思いませんでした!私はこの解決策で行くと思います:

$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    $(this).children(".item").eq(i).addClass("active");
});
4

5 に答える 5

3
$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
    currentLine.find(".item").each(function(k) {
        $(".pricetable .righttable .line .item").eq(i).addClass("active");
    });
});

http://api.jquery.com/find/

children('.item')それらがcurrentLineの直接の子である場合は、の代わりに実行できるはずですfind('.item')。これは、DOMのトラバースと呼ばれます。このページ(http://api.jquery.com/category/traversing/)にアクセスして、そこにあるいくつかの関数の説明を読んでください。たくさんトラバースする場合は、覚えておくと非常に便利です:)

于 2011-09-08T12:11:10.433 に答える
2
$('.pricetable .righttable .line:not(.blank)').find('.item').each(function() {
  // this point to child element - item
  // use $(this).addClass('active');
});
于 2011-09-08T12:11:28.147 に答える
2

itemそれぞれのn番目で何かをしたい場合はline、見た目よりもはるかに簡単です。

var index = 3; // the third "item" element 
$(".pricetable .righttable .line:not(.blank) .item:nth-child(" + index + ")")
  .addClass("active");

実際の動作をご覧ください

上記では不十分なことを他にやりたいことはありますか?

于 2011-09-08T12:11:39.890 に答える
2

私があなたの質問に答える前に少し改善します:

var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);

と簡単に交換できます:

var currentLine = $(this);

.item次に、の中に3番目の要素を見つけたい場合は$(this).find()

.find(".item:nth-child(3)");

行を繰り返す必要はありません。行を反復処理する必要がある場合は、(JavaScriptは0からカウントされるため)と比較kするだけです。2

于 2011-09-08T12:12:56.230 に答える
1
$(".pricetable .righttable .line:not(.blank)").each(function() {
  $(this).find('.item:nth-child(2)').addClass('active');
});
于 2011-09-08T12:16:37.663 に答える