.replaceWith( newContent )
引数
- target
- htmlString | Element | Array | jQuery
戻り値
- object
- jQuery オブジェクト。
jQueryオブジェクトにセットされている要素を newContent で置換する。戻り値は削除された要素がセットされたjQueryオブジェクト
サンプル
実行結果
C#
PHP
Javascript
Perl
Ruby
Java
削除された要素
Javascript
$('.jq-sample1 :button').on('click', function(){
$('.jq-sample1 p')
.replaceWith('<div>replaceAll</div>')
.appendTo('.jq-sample1 .display');
});
HTML
<div class="jq-sample1">
<button class="cq-btn m-primary">execute</button>
<p>C#</p>
<p>PHP</p>
<p>Javascript</p>
<p>Perl</p>
<p>Ruby</p>
<p>Java</p>
<h5 class="headline-output">削除された要素</h5>
<div class="display"></div>
</div>
.replaceWith( function )
引数
- function
- この関数が返す要素で置換される
戻り値
- object
- jQuery オブジェクト。
サンプル
実行結果
C#
PHP
Javascript
Perl
Ruby
Java
削除された要素
Javascript
$('.jq-sample2 :button').on('click', function(){
$('.jq-sample2 p')
.replaceWith(function (){
// this は 置換される DOMエレメント
return $('<div>', {text: $(this).attr('class')});
})
.appendTo('.jq-sample2 .display');
});
HTML
<div class="jq-sample2">
<button class="cq-btn m-primary">execute</button>
<p class="a">C#</p>
<p class="b">PHP</p>
<p class="c">Javascript</p>
<p class="d">Perl</p>
<p class="e">Ruby</p>
<p class="f">Java</p>
<h5 class="headline-output">削除された要素</h5>
<div class="display"></div>
</div>