热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> AJAX编程>>正文内容

javascript自动标记搜索结果页的关键字

发布时间:2010年05月27日点击数: 佚名
  1. <html> 
  2. <head> 
  3. <style> 
  4. /*定义样式*/ 
  5. .hilite1{background:#69c;} 
  6. </style> 
  7. <script language="javascript" type="text/javascript"> 
  8.     Hilite = { 
  9.         exact: true, 
  10.         max_nodes: 1000, 
  11.         style_name: 'hilite', 
  12.         style_name_suffix: true 
  13.     }; 
  14.     Hilite.hiliteElement = function (elm, query) { 
  15.         if (!query || elm.childNodes.length == 0) 
  16.             return; 
  17.         queryquery = query.split(/[\s,\+\.]+/); 
  18.         var qre = new Array(); 
  19.         for (var i = 0; i < query.length; i++) { 
  20.             query[i] = query[i].toLowerCase(); 
  21.             if (Hilite.exact) 
  22.                 qre.push('\\b' + query[i] + '\\b'); 
  23.             else 
  24.                 qre.push(query[i]); 
  25.         } 
  26.         qre = new RegExp(qre.join("|"), "i"); 
  27.         var stylemapper = {}; 
  28.         for (var i = 0; i < query.length; i++) { 
  29.             if (Hilite.style_name_suffix) 
  30.                 stylemapper[query[i]] = Hilite.style_name + (i + 1); 
  31.             else 
  32.                 stylemapper[query[i]] = Hilite.style_name; 
  33.         } 
  34.         var textproc = function (node) { 
  35.             var match = qre.exec(node.data); 
  36.             if (match) { 
  37.                 var val = match[0]; 
  38.                 var k = ''
  39.                 var nodenode2 = node.splitText(match.index); 
  40.                 var node3 = node2.splitText(val.length); 
  41.                 var span = node.ownerDocument.createElement('SPAN'); 
  42.                 node.parentNode.replaceChild(span, node2); 
  43.                 span.className = stylemapper[val.toLowerCase()]; 
  44.                 span.appendChild(node2); 
  45.                 return span; 
  46.             } else { 
  47.                 return node; 
  48.             } 
  49.         }; 
  50.         Hilite.walkElements(elm.childNodes[0], 1, textproc); 
  51.     }; 
  52.     Hilite.walkElements = function (node, depth, textproc) { 
  53.         var skipre = /^(script|style|textarea)/i; 
  54.         var count = 0
  55.         while (node && depth > 0) { 
  56.             count++; 
  57.             if (count >= Hilite.max_nodes) { 
  58.                 var handler = function () { 
  59.                     Hilite.walkElements(node, depth, textproc); 
  60.                 }; 
  61.                 setTimeout(handler, 50); 
  62.                 return; 
  63.             } 
  64.             if (node.nodeType == 1) { 
  65.                 if (!skipre.test(node.tagName) && node.childNodes.length > 0) { 
  66.                     nodenode = node.childNodes[0]; 
  67.                     depth++; 
  68.                     continue; 
  69.                 } 
  70.             } else if (node.nodeType == 3) { 
  71.                 node = textproc(node); 
  72.             } 
  73.             if (node.nextSibling) { 
  74.                 nodenode = node.nextSibling; 
  75.             } else { 
  76.                 while (depth > 0) { 
  77.                     nodenode = node.parentNode; 
  78.                     depth--; 
  79.                     if (node.nextSibling) { 
  80.                         nodenode = node.nextSibling; 
  81.                         break; 
  82.                     } 
  83.                 } 
  84.             } 
  85.         } 
  86.     }; 
  87.     Hilite.autoForSeachEngine = function () {  
  88.         var re = new RegExp('[\\?&](q|keyword|qk|wd|p)=([^&]+)','ig'); 
  89.         var ref = document.referrer || 'http://search.yahoo.com/search?p=javascript&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701'; 
  90.         addLoadEvent(function () { 
  91.             var match = re.exec(ref); 
  92.             if (match) { 
  93.                 var query = decodeURIComponent(match[2]); 
  94.                  Hilite.hiliteElement(document.getElementById('divMain'), query);  
  95.             } 
  96.         }); 
  97.     }; 
  98.     function addLoadEvent(f) { 
  99.         var old = window.onload; 
  100.         if (typeof old != 'function') window.onload = f
  101.         else { window.onload = function () { old(); f(); }; } 
  102.     } 
  103.    
  104.     Hilite.autoForSeachEngine(); 
  105. </script> 
  106. </head> 
  107. <body> 
  108. <div id="divMain"> 
  109. 我是test,我要被highlight。我们是javascript,我们是javascript 
  110. </div> 
  111. </body> 
  112. </html> 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作