JS highlight Method 1:
CopyCode The Code is as follows: <SCRIPT>
Function highlight (nword ){
If (nword! = ''){
VaR keyword = Document. Body. createTextRange ();
While (keyword. findtext (nword )){
Keyword. pastehtml ("<span style = 'color: red; '>" + keyword. Text + "</span> ");
Keyword. movestart ('character ', 1 );
}
}
}
Function highword (nword ){
VaR array = nword. Split (",");
For (VAR I = 0; I <array. length; I ++ ){
Highlight (array [I]);
}
}
</SCRIPT>
<Body onload = "highword ('Li, jin')">
<P> liujinzhong liuzhong </P>
</Body>
JS highlight Method 2:
2.Copy codeThe Code is as follows: <HTML>
<Head>
<Title> test page </title>
<SCRIPT type = "text/JavaScript">
<! --
Function highlight (Ele, keys)
{
VaR Reg = new Regexp ("(" + keys. Replace (/,/, "|") + ")", "G ");
Ele. innerhtml = ELE. innerhtml. Replace (Reg, "<font color = \" Red \ "> $1 </font> ");
}
Window. onload = function ()
{
Highlight (document. getelementsbytagname ("p") [0], "Li, Jin ");
}
// -->
</SCRIPT>
</Head>
<Body>
<P> liujinzhong liuzhong </P>
</Body>
</Html>