復制代碼 代碼如下:
//高亮顯示搜索到的關鍵字
function HeightLight(Keyword)
{
//文本選擇器
var TextRange;
//是否找到
var Found=false;
//找到的次數
var Count = 0;
TextRange = document.body.createTextRange();
Found = TextRange.findText(Keyword);
if (Found)
{
Count++;
}
while (Found && Count > 0)
{
TextRange.pasteHTML('<span style="background:yellow">' + Keyword + '</span>');
//將滾動條定位到第一次查到的視口范圍內
if(Count==1)
{
TextRange.scrollIntoView();
}
//繼續查找
Found = TextRange.findText(Keyword);
if (!Found)
{
Count = 0;
}
else
{
Count++;
}
}
}
//正則式法
function highLight(ele,keys)
{
var reg = new RegExp("(" + keys.replace(/,/,"|") + ")","g");
ele.innerHTML = ele.innerHTML.replace(reg,"<font color=\"red\">$1</font>");
}