1

私はこれを試していました:

$(document).ready(function () {
    $(".qa-a-count").appendTo(".qa-q-item-main");
});

しかし、多くの.qa-a-countand .qa-q-item-maindivがあります。それらは互いに追加されてしまいます。親 div () にのみ追加するようにするにはどうすればよい.qa-q-item-main divですか?

4

2 に答える 2

3
$('.qa-a-count').each(function() {
   // .parent() if this is a direct child of `qa-q-item-main`
   $(this).appendTo($(this).closest('.qa-q-item-main')); 
});

これはそれぞれを通過.qa-a-countし、その祖先に追加されます。

于 2012-05-24T08:17:09.313 に答える
1
$(".qa-a-count").each(function (){
    // append this- (the current ".qa-a-count") 
    // to it's closest ".qa-q-item-main" element.
    $(this).appendTo($(this).closest(".qa-q-item-main"));
});

またはキャッシュ$(this)

$(".qa-a-count").each(function (){
    var $this = $(this);
    $this.appendTo($this.closest(".qa-q-item-main"));
});

ただし、膨大な数の要素を反復処理している場合は、パフォーマンスが大幅に向上するわけではありません。
「$(this)」のコストはいくらですか?

于 2012-05-24T08:17:30.553 に答える