2016-03-17 118 views
1

我不得不更换所有一个div一些字符,但是当我运行的代码,该函数替换我也HTML标记字符JQuery的替换所有内容在一个div除了标签

我的代码是:

$("#main").children().each(function() { 
    $(this).html($(this).html().replace(/s/g, "K")); 
}) 
<div id="main"> 
<p>Some contents that will be replaced... <span>this will not be in a span tag :(</span></p> 
</div> 

结果:

<div id="main"> 
<p>Kome contentK that will be replaced... <Kpan>this will not be in a Kpan tag :(</Kpan></p> 
</div> 

回答

3

如果你的目标是保持专区内所有的标记,但所有这些标签内替换文本,这是困难的。您只需查找文本节点并找到其他节点,然后分别在每个文本节点内进行替换。

此问题可能有帮助:How do I select text nodes with jQuery?

相关问题