0

重複の可能性:
jQueryでテキスト文字列を見つけて太字にする

HTMLページにいくつかのテキストがあります(文字にアクセントがあります):

<p>
some téxt, some text, some téxt, some text, some text, some téxt, 
</p>

私の目標は、「テキスト」と「テキスト」のすべての出現に対して太字のスタイルを設定することです

出来ますか ?

ありがとうございました

バレリアン

4

2 に答える 2

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
于 2012-11-05T14:58:07.087 に答える
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(){...}

于 2012-11-05T14:43:41.970 に答える