Home  >  Article  >  Web Front-end  >  How to implement the highlight effect of js text content_javascript skills

How to implement the highlight effect of js text content_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:30:501416browse
Copy code The code is as follows:

//Highlight the searched keywords
function HeightLight (Keyword)
{
//Text selector
var TextRange;
//Whether it is found
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 '');
//Will The scroll bar is positioned within the viewport range found for the first time
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");
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn