惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - cncxz(虫虫)

在.NET环境下为网站增加IP过滤功能 推荐几个基于.net的cms系统 超赞的10个Windows Live Writer插件 发布ASP.NET应用程序时的10个好习惯(转) 一个基于web的pdf浏览器 ajax跨域访问代理文件下载(asp、php、asp.net) jQuery UI 1.0 已于2007-9-16日正式发布 Visual Studio插件大搜索(60+) 推荐一批基于web的开源html文本编辑器(40+) 终结IE6下背景图片闪烁问题 一个web应用程序统计在线用户列表的东东(带c#源码) 中了网络流氓www.e-jok.cn的招 Div+Css酷站一箩筐 在win2k3上使用卡巴斯基6.0 发布控件Express.NET.WindowsForms运行时的本地化补丁 创建为ClickOnce清单签名的.pfx格式数字证书 Windows Live Messenger去广告补丁 一个阴历阳历互相转化的类(c#源码) 小发现:sql2005中的异常处理消息框可以直接使用
使用 javascript 标记高亮关键词
cncxz(虫虫) · 2007-07-15 · via 博客园 - cncxz(虫虫)

一个将指定html标签内特定关键词高亮显示的javascript 函数。


    
/*----------------------------------------*\ 
     * 使用 js 标记高亮关键词 by markcxz(markcxz@aol.com)
     * 参数说明: 
     * obj: 对象, 要进行高亮显示的html标签节点. 
     * hlWords: 字符串, 要进行高亮的关键词词, 使用 竖杠(|)或空格 分隔多个词 . 
     * cssClass: 字符串, 定义关键词突出显示风格的css伪类. 
     * 参考资料: javascript HTML DOM 高亮显示页面特定字词 By shawl.qiu
    \*----------------------------------------
*/ 
    
function MarkHighLight(obj,hlWords,cssClass){

            hlWords

=AnalyzeHighLightWords(hlWords);if(obj==null || hlWords.length==0)
            
return;
        
if(cssClass==null)
            cssClass
="highlight";
        MarkHighLightCore(obj,hlWords);
//------------执行高亮标记的核心方法----------------------------
        function MarkHighLightCore(obj,keyWords){
            
var re=new RegExp(keyWords, "i"); for(var i=0; i<obj.childNodes.length; i++){var childObj=obj.childNodes[i];
                
if(childObj.nodeType==3){
                    
if(childObj.data.search(re)==-1)continue
                    
var reResult=new RegExp("("+keyWords+")""gi"); 
                    
var objResult=document.createElement("span");
                    objResult.innerHTML
=childObj.data.replace(reResult,"<span class='"+cssClass+"'>$1</span>");                     
                    
if(childObj.data==objResult.childNodes[0].innerHTML) continue
                    obj.replaceChild(objResult,childObj);                                      
                }
else if(childObj.nodeType==1){
                    MarkHighLightCore(childObj,keyWords);
                }
            }
        }        
//----------分析关键词----------------------
        function AnalyzeHighLightWords(hlWords)
        {
            
if(hlWords==nullreturn "";
            hlWords
=hlWords.replace(/\s+/g,"|").replace(/\|+/g,"|");            
            hlWords
=hlWords.replace(/(^\|*)|(\|*$)/g, "");if(hlWords.length==0return "";
            
var wordsArr=hlWords.split("|"); if(wordsArr.length>1){
                
var resultArr=BubbleSort(wordsArr);
                
var result="";
                
for(var i=0;i<resultArr.length;i++){
                    result
=result+"|"+resultArr[i];
                }                
                
return result.replace(/(^\|*)|(\|*$)/g, "");

            }

else{
                
return hlWords;
            } 
        }    
//-----利用冒泡排序法把长的关键词放前面-----    
        function BubbleSort(arr){        
            
var temp, exchange;    
            
for(var i=0;i<arr.length;i++){            
                exchange
=false;                
                
for(var j=arr.length-2;j>=i;j--){                
                    
if((arr[j+1].length)>(arr[j]).length){                    
                        temp
=arr[j+1]; arr[j+1]=arr[j]; arr[j]=temp;
                        exchange
=true;
                    }
                }                
                
if(!exchange)break;
            }
            
return arr;            
        }

        }

//----------------end------------------------

在线演示