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

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


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

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

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

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


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

函数 fHl(o, flag, rndColor, url) 源码及使用演示: 
  1. linenum
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns=" http://www.w3.org/1999/xhtml">
  4. <!-- DW6 -->
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <title>shawl.qiu template</title>
  8. <script type="text/javascript">
  9. //<![CDATA[
  10.     οnlοad=function(){
  11.         fHl(document.body, '纸伞|她');
  12.         fHl(document.body, '丁香|雨巷', true);
  13.         
  14.         fHl(document.body, '希望|愁怨', false, '/');
  15.         fHl(document.getElementById('at_main'), '独自|飘过|悠长', true, '/');
  16.         
  17. /*         fHl(document.body, '纸伞');
  18.         fHl(document.body, '她', false, '/');
  19.         fHl(document.body, '丁香', true, '/');
  20.         fHl(document.body, '雨巷', true, '/');
  21.         
  22.         fHl(document.body, '希望', false);
  23.         fHl(document.body, '愁怨', true); */
  24.         
  25. /*         fHl(document.body, '丁香', 'blue', 'white', '/');
  26.         fHl(document.body, '雨巷', 'gray', 'white', '/');
  27.         fHl(document.body, '独自', 'white', 'red');
  28.         fHl(document.body, '悠长', 'white', 'red');
  29.         fHl(document.body, '飘过'); */
  30.     }
  31.     /*----------------------------------------*/
  32.      * 使用 javascript HTML DOM 高亮显示页面特定字词 By shawl.qiu
  33.      * 参数说明:
  34.      * o: 对象, 要进行高亮显示的对象.
  35.      * flag: 字符串, 要进行高亮的词或多个词, 使用 竖杠(|) 分隔多个词 .
  36.      * rndColor: 布尔值, 是否随机显示文字背景色与文字颜色, true 表示随机显示.
  37.      * url: URI, 是否对高亮的词添加链接. 
  38.     /*----------------------------------------*/
  39.     //--------begin function fHl(o, flag, rndColor, url)------------------//
  40.     function fHl(o, flag, rndColor, url){
  41.         var bgCor=fgCor='';
  42.         if(rndColor){
  43.             bgCor=fRndCor(10, 20);
  44.             fgCor=fRndCor(230, 255);
  45.         } else {
  46.             bgCor='yellow';
  47.             fgCor='black';
  48.         }
  49.         var re=new RegExp(flag, 'i');
  50.         for(var i=0; i<o.childNodes.length; i++){    
  51.             var o_=o.childNodes[i];
  52.             var o_p=o_.parentNode;
  53.             if(o_.nodeType==1) {
  54.                 fHl(o_, flag, rndColor, url);                
  55.              } else if (o_.nodeType==3) {
  56.                 if(!(o_p.nodeName=='A')){
  57.                     if(o_.data.search(re)==-1)continue;
  58.                     var temp=fEleA(o_.data, flag);
  59.                     o_p.replaceChild(temp, o_);
  60.                 }
  61.             }  // shawl.qiu script
  62.         }
  63.         //------------------------------------------------
  64.         function fEleA(text, flag){
  65.             var style=' style="background-color:'+bgCor+';color:'+fgCor+';" '
  66.             var o=document.createElement('span');
  67.             var str='';
  68.             var re=new RegExp('('+flag+')', 'gi');
  69.             if(url){
  70.                 str=text.replace(re, '<a href="'+url+
  71.                 '"'+style+'>$1</a>');
  72.             } else {
  73.                 str=text.replace(re, '<span '+style+'>$1</span>');
  74.             }
  75.             o.innerHTML=str;
  76.             return o;
  77.         }     // shawl.qiu script
  78.         //------------------------------------------------
  79.         function fRndCor(under, over){
  80.             if(arguments.length==1){
  81.                 var over=under;
  82.                     under=0;
  83.             }else if(arguments.length==0){
  84.                 var under=0;
  85.                 var over=255;
  86.             }
  87.             var r=fRandomBy(under, over).toString(16);
  88.                 r=padNum(r, r, 2);
  89.             var g=fRandomBy(under, over).toString(16);
  90.                 g=padNum(g, g, 2);
  91.             var b=fRandomBy(under, over).toString(16);
  92.                 b=padNum(b, b, 2);
  93.                 //defaultStatus=r+' '+g+' '+b
  94.             return '#'+r+g+b;
  95.             function fRandomBy(under, over){
  96.                 switch(arguments.length){
  97.                     case 1: return parseInt(Math.random()*under+1);
  98.                     case 2: return parseInt(Math.random()*(over-under+1) + under);
  99.                     default: return 0;
  100.                 }
  101.             } // shawl.qiu script
  102.             function padNum(str, num, len){
  103.                 var temp=''
  104.                 for(var i=0; i<len;temp+=num, i++);
  105.                 return temp=(temp+=str).substr(temp.length-len);
  106.             } // shawl.qiu script
  107.         }
  108.     } // shawl.qiu script
  109.     //--------end function fHl(o, flag, rndColor, url)--------------------//
  110. //]]>
  111. </script>
  112. </head>
  113. <body>
  114. <div class="at_main" id="at_main"><p/><b>CITE:</b><cite><div class=u_cite>戴望舒写女孩<br/>
  115. <br/>
  116. &nbsp;雨  巷&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. 消了她的颜色,&nbsp;<br/>
  149. 散了<a href="/">她</a>的芬芳,&nbsp;<br/>
  150. 消散了,甚至她的&nbsp;<br/>
  151. 叹息般的眼光&nbsp;<br/>
  152. 丁香般的惆怅。&nbsp;<br/>
  153. 撑着油纸伞,独自&nbsp;<br/>
  154. 彷徨在悠长、悠长&nbsp;<br/>
  155. 又寂寥的雨巷,&nbsp;<br/>
  156. 我希望飘过&nbsp;<br/>
  157. 一个丁香一样地&nbsp;<br/>
  158. 结着愁怨的姑娘。</div></cite></div>
  159. <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>
  160. </body>
  161. </html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值