1

したがって、私の dom<div id="test">random text goes here</div>は div を生成します。次のように、この div 内にスパン タグを jquery で挿入できます。<div id="test"><span>random text goes here</span></div>

4

6 に答える 6

4

wrapInner以下のような関数を使ってみてください。

$('#test').wrapInner('<span/>');
于 2012-05-18T17:37:23.453 に答える
1

または、内部のものを完全に制御したい場合(メソッドにもいくつかの変更を加えたい場合など)、次のように使用できます。

$("div#test").append(function () {
    //you can refer to the object you are making modifications in as 'this'
    var txt = $(this).text();
    //do your modifications for the 'txt' if needed
    //..
    //finally use return statement to return the value or
    //object you need to the original append function
    //$("<span>") creates a span html element
    return $("<span>").text(txt);
});

この質問は、wrapInner() メソッドを使用する多くの人によって回答されています。関数内で「魔法」を作成する方法をお見せしたかっただけです。

于 2012-05-18T17:43:46.747 に答える
1

wrapInner()メソッドを探しています。

于 2012-05-18T17:37:12.233 に答える
1

絶対に、チェックしてください$.wrapInner()

ラッピング要素

/* Wrap an HTML structure around the content of each matched element */
$("#test").wrapInner("<span>");

コールバック関数

または、無名関数を使用して、もう少し制御することもできます。

$("#test").wrapInner(function() {
  return '<span class="' + this.id + '" />';
});

idクラス名として値を適用します:

<div id="test">
  <span class="test">random text goes here</span>
</div>
于 2012-05-18T17:37:39.087 に答える
1

$.wrapInnerこのように使う

$('#test').wrapInner('<span/>');
于 2012-05-18T17:38:13.583 に答える
0

他の人が答えたように、使用できますwrapInnerhtmlそれ以外の場合は、メソッドを使用するだけで実行できます

$('#test').html($('<span>').html($('#test').html()));

フィドル: http://jsfiddle.net/zcg9B/

于 2012-05-18T17:44:20.807 に答える