Sta*_*lov 3 html jquery replace
我有以下代码:
<div class="normal">
<div>
<div>~Content 1~</div>
</div>
</div>
<div class="normal replace">
<a href="#">
<div>~Content 2~</div>
</a>
</div>
Run Code Online (Sandbox Code Playgroud)
使用jQuery,我想用普通标签替换<a>里面的标签,同时保持它的内容.<div class="normal replace"><div>
<div class="normal replace">
<div="result">
<div>~Content 2~</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
und*_*ned 13
$('.normal > a').replaceWith(function() {
return $('<div/>', {
html: this.innerHTML
});
});
Run Code Online (Sandbox Code Playgroud)
尝试这样的事情:
// Select the 'a' tag to be replaced and call the replaceWith method
$('.normal a').replaceWith(function(){
// Execute a callback to generate contents of the replacement
// The $('<div>') part creates a div
return $('<div>', {
html: this.innerHTML // This takes the html of the 'a' tag and copies it to the new div
});
});
Run Code Online (Sandbox Code Playgroud)
jQuery replaceWith方法接受该项目,并将其替换为回调函数返回的内容。
看到这里更多信息