jquery replace tag with tag text [duplicate]

2019-05-19 00:44发布

问题:

This question already has an answer here:

  • Replace a link with it's text with jQuery 2 answers

i want to replace tags span.textReplace with the actual html

let say i've got a string dom what looks like this:

<div id="content">
    hello world <span id="query1" class="textReplace">Query 1</span><br/>this is a test <span id="query2" class="textReplace">Query 2</span> this is <b>another</b> test <span id="query3" class="textReplace">Query3</span> finished
</div>

i want the html of #content to look like this:

hello world Query1
this is a test Query2 this is another test Query3 finished.

回答1:

got it.

$("#content span.textReplace").replaceWith(function () { return $(this).text() });


回答2:

Do you mean something like:

$('span.textReplace').html("actual html");

Where is this new HTML source coming from?