【发布时间】:2012-10-20 00:57:09
【问题描述】:
我想清理我所见即所得的 html 标签。我想用另一个替换某些标签。这几乎只适用于 replaceWith() 也从标签中删除内容。我不想这样做。我只想更换标签。这是我目前所拥有的。
测试文本:
This<div>is</div><div>a test</div><div>to clean </div><div>some tags</div><div><br></div>
预期结果:
This<p>is</p><p>a test</p><p>to clean</p><p>some tags</p><p><br></p>
实际结果:
This<p></p><p></p><p></p><p></p><p></p>
这是我用来查找和替换的代码
var thebad = ["h1","h2","h3","h4","h5","h6","div"];
var thegood = ["","","","","","","<p>"];
for(var i = 0; i < thebad.length; i++){
$('.content').contents().find('body').find(thebad[i]).replaceWith(thegood[i]);
}
当我替换它们时,我需要弄清楚如何将文本保留在 html 标签内。
提前致谢
【问题讨论】:
-
你忘记了“丑”:
<script>:D(开个玩笑) -
只是指出它会从任何上述标签中删除文本,例如
Test
会导致空值,因为它也会删除单词“Test” .
标签: jquery