This article introduces how to achieve the highlighted Effect of JavaScript content. If you need it, refer to it.
The Code is as follows:
// Highlight the searched keywords
Function HeightLight (Keyword)
{
// Text Selector
Var TextRange;
// Find
Var Found = false;
// Number of times found
Var Count = 0;
TextRange = document. body. createTextRange ();
Found = TextRange. findText (Keyword );
If (Found)
{
Count ++;
}
While (Found & Count> 0)
{
TextRange. pasteHTML (''+ Keyword + '');
// Locate the scroll bar within the range of the first view.
If (Count = 1)
{
TextRange. scrollIntoView ();
}
// Continue searching
Found = TextRange. findText (Keyword );
If (! Found)
{
Count = 0;
}
Else
{
Count ++;
}
}
}
// Regular Expression Method
Function highLight (ele, keys)
{
Var reg = new RegExp ("(" + keys. replace (/,/, "|") + ")", "g ");
Ele. innerHTML = ele. innerHTML. replace (reg, "$1 ");
}