我的html页面上有一些文字(带有重音符号):
<p>
some téxt, some text, some téxt, some text, some text, some téxt,
</p>
我的目标是为所有出现的“文字”和“téxt”设置大胆的风格
有可能吗?
谢谢
Valeriane
答案 0 :(得分:0)
在您的情况下,您无法执行以下操作,
str.replace(/(t.xt)/g, "<b>"+text+"</b>);
所以我们需要使用回调函数:
//Assuming you can retrieve the text from the p tag or from the entire page
var str = "some téxt, some text, some téxt, some text, some text, some téxt, "
str.replace(/(t.xt)/g, function(match){
return "<b>"+match+"</b>";
})
您可以将此代码段添加到document.onload
或 $().function(){...}
答案 1 :(得分:0)
假设您的文字位于:
<p id="txt">
some téxt, some text, some téxt, some Text, some text, some téxt, xyz
</p>
var bold = ['some','text', 'xyz']; // words to bold
var content = document.getElementById('txt');//content to find those words
var regExp = new RegExp('('+bold.join('|')+')', "gi"); //build regular expression (i means case insensitive)
content.innerHTML = content.innerHTML.replace(regExp, "<strong>$1</strong>"); //find and bold