假设我有以下HTML结构:替换元素含量,而无需更换HTML
<test>
<div>
This is a test
</div>
<div>
This is another test
<button>
Button test
</button>
</div>
</test>
现在我使用下面的jQuery代码来代替,例如, 'T':
$("test *").each(function(index, value) {
$(this).html($(this).html().replace(new RegExp('t', "ig"), "<b>t</b>"));
});
然而,这个结果在下面的HTML结构(这是意想不到的,看到<button>
标签,它打破我的HTML):
<test>
<div>
<b>T</b>his is a <b>t</b>es<b>t</b>
</div>
<div>
<b>T</b>his is ano<b>t</b>her <b>t</b>es<b>t</b>
<bu<b>t</b><b>t</b>on>
Bu<b>t</b><b>t</b>on <b>t</b>es<b>t</b>
</bu<b>t</b><b>t</b>on>
</div>
</test>
我想实现的是:
<test>
<div>
<b>T</b>his is a <b>t</b>es<b>t</b>
</div>
<div>
<b>T</b>his is ano<b>t</b>her <b>t</b>es<b>t</b>
<button>
Bu<b>t</b><b>t</b>on <b>t</b>es<b>t</b>
</button>
</div>
</test>
基本上,我想整个元素内更换,但保留了HTML代码和所有的HTML属性。
所以,如果我理解正确的话,你不想
@ Goose,正确,但这不仅适用于按钮标签,而且适用于当然与正则表达式匹配的所有html标签。 –