Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 17:30:50
Original
1442 people have browsed it
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");
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template