1. Source Code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ("# Btn_1"). click (function (){
Var $ keyword = $ ("# Text1"). val ()
SetHeightKeyWord ('bbb ', $ keyword, 'red', true)
});
});
Function setHeightKeyWord (id, keyword, color, bold ){
If (keyword = "")
Return;
Var tempHTML = $ ("#" + id).html ();
Var htmlReg = new RegExp ("\ <.*? \> "," I ");
Var arrA = new Array ();
For (var I = 0; true; I ++ ){
Var m = htmlReg.exe c (tempHTML );
If (m ){
ArrA [I] = m;
}
Else {
Break;
}
TempHTML = tempHTML. replace (m, "[[[" + I + "]");
}
Var replaceText
If (bold)
ReplaceText = "<B style = 'color:" + color + "; '> $1 </B> ";
Else
ReplaceText = "<font style = 'color:" + color + "; '> $1 </font> ";
Var arrayWord = keyword. split (',');
For (var w = 0; w <arrayWord. length; w ++ ){
Var r = new RegExp ("(" + arrayWord [w]. replace (/[() {}. + *? ^ $ | \\\ [\]/G, "\\$ &") + ")", "ig ");
TempHTML = tempHTML. replace (r, replaceText );
}
For (var I = 0; I <arrA. length; I ++ ){
TempHTML = tempHTML. replace ("[[[" + I + "]", arrA [I]);
}
$ ("#" + Id1_.html (tempHTML );
}
</Script>