我想使用Javascript replace()添加html标记(但它会将它们作为字符串返回) [英] I want to use Javascript replace() to add html tags (but it's returning them as strings)
本文介绍了我想使用Javascript replace()添加html标记(但它会将它们作为字符串返回)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的javascript函数,可将任意span.certain_class中的"y"字符替换为"i":
I have a simple javascript function which replaces the "y" character within a any span.certain_class with an "i":
$(document).ready(function() {
$('span.certain_class').each(function() {
$(this).text($(this).text().replace(/y/ig, function(s) {
return (s === 'Y' ? 'I' : 'i');
}));
});
});
我遇到的问题是我想用跨距将替换的字符包装起来:
The problem I'm having is that I want to wrap the replaced character with a span:
<span>i</span>
执行此操作时,浏览器将标签打印为字符串,而不是将其解析为HTML代码.
When I do this, the browser prints out the tags as strings, instead of parsing them as HTML code.
有人可以帮我解决这个问题吗?
Can anyone help me solve this?
推荐答案
尝试
$(this).html($(this).text().replace........
这将使结果解析为HTML而不是纯文本.
That will make the result parse as HTML rather than plain text.
这篇关于我想使用Javascript replace()添加html标记(但它会将它们作为字符串返回)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文