使用 javascript 標記高亮關鍵詞)

來源:互聯網
上載者:User
轉自:http://www.cnblogs.com/cncxz/archive/2007/07/15/818508.html

 /*----------------------------------------*\ 
     * 使用 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==null) return "";
            hlWords=hlWords.replace(/\s+/g,"|").replace(/\|+/g,"|");            
            hlWords=hlWords.replace(/(^\|*)|(\|*$)/g, "");
            
            if(hlWords.length==0) return "";
            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------------------------

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.