使用 javascript HTML DOM 高亮显示页面特定字词 By shawl.qiu

来源:互联网 发布:苹果手机蓝牙共享网络 编辑:程序博客网 时间:2024/04/28 14:25

使用 javascript HTML DOM 高亮显示页面特定字词 By shawl.qiu


说明: 
这个主要应用于搜索结果上, 高亮显示搜索到的相关词句. 

写这个函数主要是前几天在CSDN社区JS版一个网友有这么一个需求, 当时草草写了一个简单点的函数实现高亮显示页面特定词句.

不过后来发现这个功能很不错, 就找时间完善了一下. 
最后效果还行, 唯一不足的地方在于高亮显示的时候, 同一个词可能会有多种颜色. 
其实要一个词只显示一个颜色并不难, 不过那要耗费更多的代码去判断, 因此也就不完善该功能呢.  

函数 fHl(o, flag, rndColor, url) 参数说明:
    linenum
  1. /*----------------------------------------*/
  2.  * 使用 javascript HTML DOM 高亮显示页面特定字词 By shawl.qiu
  3.  * 参数说明:
  4.  * o: 对象, 要进行高亮显示的对象.
  5.  * flag: 字符串, 要进行高亮的词或多个词, 使用 竖杠(|) 分隔多个词 .
  6.  * rndColor: 布尔值, 是否随机显示文字背景色与文字颜色, true 表示随机显示.
  7.  * url: URI, 是否对高亮的词添加链接. 
  8. /*----------------------------------------*/ 


shawl.qiu 
2006-11-12
http://blog.csdn.net/btbtd

函数 fHl(o, flag, rndColor, url) 源码及使用演示: 
    linenum
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns=" http://www.w3.org/1999/xhtml">
  3. <!-- DW6 -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>shawl.qiu template</title>
  7. <script type="text/javascript">
  8. //<![CDATA[
  9.     onload=function(){
  10.         fHl(document.body, '纸伞|她');
  11.         fHl(document.body, '丁香|雨巷', true);
  12.         
  13.         fHl(document.body, '希望|愁怨', false, '/');
  14.         fHl(document.getElementById('at_main'), '独自|飘过|悠长', true, '/');
  15.         
  16. /*         fHl(document.body, '纸伞');
  17.         fHl(document.body, '她', false, '/');
  18.         fHl(document.body, '丁香', true, '/');
  19.         fHl(document.body, '雨巷', true, '/');
  20.         
  21.         fHl(document.body, '希望', false);
  22.         fHl(document.body, '愁怨', true); */
  23.         
  24. /*         fHl(document.body, '丁香', 'blue', 'white', '/');
  25.         fHl(document.body, '雨巷', 'gray', 'white', '/');
  26.         fHl(document.body, '独自', 'white', 'red');
  27.         fHl(document.body, '悠长', 'white', 'red');
  28.         fHl(document.body, '飘过'); */
  29.     }
  30.     /*----------------------------------------*/
  31.      * 使用 javascript HTML DOM 高亮显示页面特定字词 By shawl.qiu
  32.      * 参数说明:
  33.      * o: 对象, 要进行高亮显示的对象.
  34.      * flag: 字符串, 要进行高亮的词或多个词, 使用 竖杠(|) 分隔多个词 .
  35.      * rndColor: 布尔值, 是否随机显示文字背景色与文字颜色, true 表示随机显示.
  36.      * url: URI, 是否对高亮的词添加链接. 
  37.     /*----------------------------------------*/
  38.     //--------begin function fHl(o, flag, rndColor, url)------------------//
  39.     function fHl(o, flag, rndColor, url){
  40.         var bgCor=fgCor='';
  41.         if(rndColor){
  42.             bgCor=fRndCor(10, 20);
  43.             fgCor=fRndCor(230, 255);
  44.         } else {
  45.             bgCor='yellow';
  46.             fgCor='black';
  47.         }
  48.         var re=new RegExp(flag, 'i');
  49.         for(var i=0; i<o.childNodes.length; i++){    
  50.             var o_=o.childNodes[i];
  51.             var o_p=o_.parentNode;
  52.             if(o_.nodeType==1) {
  53.                 fHl(o_, flag, rndColor, url);                
  54.              } else if (o_.nodeType==3) {
  55.                 if(!(o_p.nodeName=='A')){
  56.                     if(o_.data.search(re)==-1)continue;
  57.                     var temp=fEleA(o_.data, flag);
  58.                     o_p.replaceChild(temp, o_);
  59.                 }
  60.             }  // shawl.qiu script
  61.         }
  62.         //------------------------------------------------
  63.         function fEleA(text, flag){
  64.             var style=' style="background-color:'+bgCor+';color:'+fgCor+';" '
  65.             var o=document.createElement('span');
  66.             var str='';
  67.             var re=new RegExp('('+flag+')', 'gi');
  68.             if(url){
  69.                 str=text.replace(re, '<a href="'+url+
  70.                 '"'+style+'>$1</a>');
  71.             } else {
  72.                 str=text.replace(re, '<span '+style+'>$1</span>');
  73.             }
  74.             o.innerHTML=str;
  75.             return o;
  76.         }     // shawl.qiu script
  77.         //------------------------------------------------
  78.         function fRndCor(under, over){
  79.             if(arguments.length==1){
  80.                 var over=under;
  81.                     under=0;
  82.             }else if(arguments.length==0){
  83.                 var under=0;
  84.                 var over=255;
  85.             }
  86.             var r=fRandomBy(under, over).toString(16);
  87.                 r=padNum(r, r, 2);
  88.             var g=fRandomBy(under, over).toString(16);
  89.                 g=padNum(g, g, 2);
  90.             var b=fRandomBy(under, over).toString(16);
  91.                 b=padNum(b, b, 2);
  92.                 //defaultStatus=r+' '+g+' '+b
  93.             return '#'+r+g+b;
  94.             function fRandomBy(under, over){
  95.                 switch(arguments.length){
  96.                     case 1: return parseInt(Math.random()*under+1);
  97.                     case 2: return parseInt(Math.random()*(over-under+1) + under);
  98.                     default: return 0;
  99.                 }
  100.             } // shawl.qiu script
  101.             function padNum(str, num, len){
  102.                 var temp=''
  103.                 for(var i=0; i<len;temp+=num, i++);
  104.                 return temp=(temp+=str).substr(temp.length-len);
  105.             } // shawl.qiu script
  106.         }
  107.     } // shawl.qiu script
  108.     //--------end function fHl(o, flag, rndColor, url)--------------------//
  109. //]]>
  110. </script>
  111. </head>
  112. <body>
  113. <div class="at_main" id="at_main"><p/><b>CITE:</b><cite><div class=u_cite>戴望舒写女孩<br/>
  114. <br/>
  115. &nbsp;雨  巷&nbsp;<br/>
  116. 撑着油纸伞,独自&nbsp;<br/>
  117. 彷徨在悠长、悠长&nbsp;<br/>
  118. 又寂寥的雨巷,&nbsp;<br/>
  119. 我希望逢着&nbsp;<br/>
  120. 一个丁香一样地&nbsp;<br/>
  121. 结着愁怨的姑娘。&nbsp;<br/>
  122. 她是有&nbsp;<br/>
  123. 丁香一样的颜色,&nbsp;<br/>
  124. 丁香一样的芬芳,&nbsp;<br/>
  125. 丁香一样的忧愁,&nbsp;<br/>
  126. 在雨中哀怨,&nbsp;<br/>
  127. 哀怨又彷徨;&nbsp;<br/>
  128. 她彷徨在这寂寥的雨巷,&nbsp;<br/>
  129. 撑着油纸伞&nbsp;<br/>
  130. 像我一样,&nbsp;<br/>
  131. 像我一样地&nbsp;<br/>
  132. 默默踟躇着&nbsp;<br/>
  133. 冷漠、凄清,又惆怅。&nbsp;<br/>
  134. 她默默地走近,&nbsp;<br/>
  135. 走近,又投出&nbsp;<br/>
  136. 叹息一般的眼光&nbsp;<br/>
  137. 她飘过&nbsp;<br/>
  138. 像梦一般地,&nbsp;<br/>
  139. 像梦一般地凄婉迷茫。&nbsp;<br/>
  140. 像梦中飘过&nbsp;<br/>
  141. 一枝丁香地,&nbsp;<br/>
  142. 我身旁飘过这个女郎;&nbsp;<br/>
  143. 她默默地远了,远了,&nbsp;<br/>
  144. 到了颓圮的篱墙,&nbsp;<br/>
  145. 走尽这雨巷。&nbsp;<br/>
  146. 在雨的哀曲里,&nbsp;<br/>
  147. 消了她的颜色,&nbsp;<br/>
  148. 散了<a href="/">她</a>的芬芳,&nbsp;<br/>
  149. 消散了,甚至她的&nbsp;<br/>
  150. 叹息般的眼光&nbsp;<br/>
  151. 丁香般的惆怅。&nbsp;<br/>
  152. 撑着油纸伞,独自&nbsp;<br/>
  153. 彷徨在悠长、悠长&nbsp;<br/>
  154. 又寂寥的雨巷,&nbsp;<br/>
  155. 我希望飘过&nbsp;<br/>
  156. 一个丁香一样地&nbsp;<br/>
  157. 结着愁怨的姑娘。</div></cite></div>
  158. <span class="left160px"><a href="article.asp?classid=14&nclassid=178&articleid=12830#anchor">戴望舒写女孩</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=12819#anchor">悲惨世界 - 第四部 卜吕梅街的儿女情和圣德尼街的英雄血 - 第五卷 结尾不象开头 - 四 石头下面的一颗心</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=12835#anchor">青玉案&nbsp;元夕</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=12855#anchor">“科学精神”语义分析</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=3053#anchor">再别康桥 --徐志摩</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=12862#anchor">学术论文格式</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=12836#anchor">一棵开花的树</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=12840#anchor">书信写作格式</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=12818#anchor">悲惨世界 - 第四部 卜吕梅街的儿女情和圣德尼街的英雄血 - 第十二卷 科林斯 - 六 等 待</a><br/><a href="article.asp?classid=14&nclassid=178&articleid=12834#anchor">卿云歌</a></span>
  159. </body>
  160. </html>


原创粉丝点击