javascrpt:搜索关键字着色

http://www.kryogenix.org/code/browser/searchhi/

http://webdesign.weisshart.de/kryogenix.php

http://www.kryogenix.org/code/browser/searchhi/searchhi.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified 20070316 to stop highlighting inside nosearchhi nodes */
/* Modified 20081217 to do in-page searching and wrap up in an object */
/* Modified 20081218 to scroll to first hit like
   http://www.woolyss.free.fr/js/searchhi_Woolyss.js and say when not found */
 
searchhi = {
  highlightWord: function(node,word) {
    // Iterate into this nodes childNodes
    if (node.hasChildNodes) {
        for (var hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
            searchhi.highlightWord(node.childNodes[hi_cn],word);
        }
    }
 
    // And do this node itself
    if (node.nodeType == 3) { // text node
        tempNodeVal = node.nodeValue.toLowerCase();
        tempWordVal = word.toLowerCase();
        if (tempNodeVal.indexOf(tempWordVal) != -1) {
            var pn = node.parentNode;
            // check if we're inside a "nosearchhi" zone
            var checkn = pn;
            while (checkn.nodeType != 9 &&
            checkn.nodeName.toLowerCase() != 'body') {
            // 9 = top of doc
                if (checkn.className.match(/\bnosearchhi\b/)) { return; }
                checkn = checkn.parentNode;
            }
            if (pn.className != "searchword") {
                // word has not already been highlighted!
                var nv = node.nodeValue;
                var ni = tempNodeVal.indexOf(tempWordVal);
                // Create a load of replacement nodes
                var before = document.createTextNode(nv.substr(0,ni));
                var docWordVal = nv.substr(ni,word.length);
                var after = document.createTextNode(nv.substr(ni+word.length));
                var hiwordtext = document.createTextNode(docWordVal);
                var hiword = document.createElement("span");
                hiword.className = "searchword";
                hiword.appendChild(hiwordtext);
                pn.insertBefore(before,node);
                pn.insertBefore(hiword,node);
                pn.insertBefore(after,node);
                pn.removeChild(node);
                searchhi.found += 1;
                if (searchhi.found == 1) pn.scrollIntoView();
            }
        }
    }
  },
 
  googleSearchHighlight: function() {
    var ref = document.referrer;
    if (ref.indexOf('?') == -1) return;
    var qs = ref.substr(ref.indexOf('?')+1);
    var qsa = qs.split('&');
    for (var i=0;i<qsa.length;i++) {
        var qsip = qsa[i].split('=');
      if (qsip.length == 1) continue;
      if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google, p= for Yahoo
            var wordstring = unescape(qsip[1].replace(/\+/g,' '));
            searchhi.process(wordstring);
      }
    }
  },
   
  process: function(wordstring) {
    searchhi.found = 0;
    var words = wordstring.split(/\s+/);
    for (w=0;w<words.length;w++) {
        searchhi.highlightWord(document.getElementsByTagName("body")[0],words[w]);
    }
    if (searchhi.found === 0) {
      searchhi.nohits();
    }
  },
   
  nohits: function() {
  },
   
  init: function() {
    if (!document.createElement || !document.getElementsByTagName) return;
    // hook up forms of type searchhi
    var frms = document.getElementsByTagName("form");
    for (var i=0; i<frms.length; i++) {
      if (frms[i].className.match(/\bsearchhi\b/)) {
        frms[i].onsubmit = function() {
          var inps = this.getElementsByTagName("input");
          for (var j=0; j<inps.length; j++) {
            if (inps[j].type == "text") {
              searchhi.process(inps[j].value);
              return false;
            }
          }
        };
      }
    }
    // highlight search engine referrer results
    searchhi.googleSearchHighlight();
  }
};
 
(function(i) {var u =navigator.userAgent;var e=/*@cc_on!@*/false; var st =
setTimeout;if(/webkit/i.test(u)){st(function(){var dr=document.readyState;
if(dr=="loaded"||dr=="complete"){i()}else{st(arguments.callee,10);}},10);}
else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){
document.addEventListener("DOMContentLoaded",i,false); } else if(e){     (
function(){var t=document.createElement('doc:rdy');try{t.doScroll('left');
i();t=null;}catch(e){st(arguments.callee,0);}})();}else{window.onload=i;}})(searchhi.init);

  searchhi.vbs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Function DeCodeAnsi(s)
  Dim i, sTmp, sResult, sTmp1
  sResult = ""
  For i=1 To Len(s)
    If Mid(s,i,1)="%" Then
      sTmp = "&H" & Mid(s,i+1,2)
      If isNumeric(sTmp) Then
        If CInt(sTmp)=0 Then
          i = i + 2
        ElseIf CInt(sTmp)>0 And CInt(sTmp)<128 Then
          sResult = sResult & Chr(sTmp)
          i = i + 2
        Else
          If Mid(s,i+3,1)="%" Then
            sTmp1 = "&H" & Mid(s,i+4,2)
            If isNumeric(sTmp1) Then
              sResult = sResult & Chr(CInt(sTmp)*16*16 + CInt(sTmp1))
              i = i + 5
            End If
          Else
            sResult = sResult & Chr(sTmp)
            i = i + 2
          End If
        End If
      Else
        sResult = sResult & Mid(s,i,1)
      End If
    Else
      sResult = sResult & Mid(s,i,1)
    End If
  Next
  DeCodeAnsi = sResult
End Function

  

posted @   ®Geovin Du Dream Park™  阅读(257)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示