2012-03-30 6 views
0

jQueryの親要素に複数の連結要素を追加したいのですが、私が使用できる最もクリーンなコードです。今、私はこのように、親に新しい要素を付加しています:jQueryで連結された要素をオプションでどのように前置するのですか?

HTML

<div id="container"> 
</div> 

のjQuery:

$("<div>", { 
    class: "some-class another-class" 
}) 
.prependTo("#container"); 

結果のHTML

<div id="container"> 
    <div class="some-class another-class"> 
    </div> 
</div> 

私は、このように複数のconcatinated要素を挿入し、できるだけ効率的な方法でクラスと内部のテキストをそれらに適用したいと考えています。

HTML

<div id="container"> 
    <div class="some-class another-class"> 
    text: <strong><em>text</em></strong> 
    Some more text 
    <button class="class-1 class-2 right"></button> 
    <div class="clear"></div> 
    </div> 
</div> 

複数の要素でこれを行う方法がありますが、私は上記の私の例では一つの要素でそれをやっているのと同じ方法:最終的な出力は次のようになりますか?

開始コードのjsfiddleを作成しました。

答えて

0

最も簡単な方法は私の知っている:

$("<div />").addClass("some-class another-class") 
    .html("text: <strong><em>text</em></strong> 
     Some more text 
     <button class="class-1 class-2 right"></button> 
     <div class="clear"></div> 
    ).appendTo('#container'); 
ここ

$("<div>", { class: "some-class another-class" }).insertBefore("#container"); 

は、ドキュメントです

関連する問題