<div class="codetitle"> <span><a style="CURSOR: pointer" data="88215" class="copybut" id="copybut88215" onclick="doCopy('code88215')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code88215"> <br><script language="JavaScript"> <BR>function findAll(s){ <BR>if(s.length==0){ <BR>alert("请输入查询关键字"); <BR>} <br><br>s=encode(s); <BR>var TDs=document.all.DataT1.all.tags("TD"); <BR>var num=0; <BR>for(var i=0;i<TDs.length;i++){ <BR>var tdObj=TDs[i]; <BR>var obj=tdObj.childNodes[0]; <BR>if(!obj.className || obj.className!="highlight"){ <BR>var t=obj.innerHTML.replace(/<span\s+class=.?highlight.?>([^<>]*)<\/span>/gi,"$1"); <BR>obj.innerHTML=t; <BR>var cnt=loopSearch(s,obj); <BR>t=obj.innerHTML; <BR>var r=/{searchHL}(({(?!\/searchHL})|[^{])*){\/searchHL}/g; <BR>t=t.replace(r,"<span class='highlight'>$1"); <BR>obj.innerHTML=t; <BR>num=num+cnt; <BR>} <BR>} <BR>alert("查找到关键字"+num+"处"); <br><br>} <BR>function encode(s){ <BR>return s.replace(/&/g,"&").replace(/,"<").replace(/>/g,">").replace(/([\\\.\*\[\]\(\)\$\^])/g,"\\$1"); <BR>} <BR>function decode(s){ <BR>return s.replace(/\\([\\\.\*\[\]\(\)\$\^])/g,"$1").replace(/>/g,">").replace(/,"<").replace(/&/g,"&"); <BR>} <br><br>function loopSearch(s,obj){ <BR>var cnt=0; <BR>if(obj.nodeType==3){ <BR>cnt=replace(s,obj); <BR>return cnt; <BR>} <BR>for(var i=0,c;c=obj.childNodes[i];i++){ <BR>if(!c.className || c.className!="highlight") <BR>cnt+=loopSearch(s,c); <br><br>} <BR>return cnt; <BR>} <br><br>function replace(s,dest){ <BR>var r=new RegExp(s,"gi"); <BR>var tm=null; <BR>var t=dest.nodeValue; <BR>var cnt=0; <BR>var arr=new Array(); <BR>var a=""; <BR>var b=""; <BR>if(tm=t.match(r)){ <BR>cnt=tm.length; <BR>a=tm.toString(); <BR>arr=a.split(","); <BR>for(var i=0;i<arr.length;i++) <BR>b="{searchHL}"+arr[i]+"{/searchHL}"; <BR>t=t.replace(r,b); <BR>dest.nodeValue=t; <BR>} <BR>return cnt; <BR>} <br><br></script> <br><style type="text/css"> <BR>.highlight{background:blue;font-weigh:bold;color:black;} <BR></style> <br> </div> <br>以上方法需要注意的有两点:1,在findAll 中obj取到最小单位,否则的话替换会出乱,我在实践中体会这一点主要是和数据岛绑定有关,一般的文本是不需要这么小心的.2,replace方法将tm拆分成数组,也和表格有关,一般的文本不需要拆分 <br><br>如果是一般的文本可以如下写 <br><br><div class="codetitle"> <span><a style="CURSOR: pointer" data="79929" class="copybut" id="copybut79929" onclick="doCopy('code79929')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code79929"> <br><script language="JavaScript"> <BR>function findAll(s){ <BR>if (s.length==0){ <BR>alert('搜索关键词未填写!'); <BR>return false; <BR>} <BR>s=encode(s); <BR>var obj=document.getElementsByTagName("body")[0]; <BR>var t=obj.innerHTML.replace(/<span\s+class=.?highlight.?>([^<>]*)<\/span>/gi,"$1"); <BR>obj.innerHTML=t; <BR>var cnt=loopSearch(s,obj); <BR>t=obj.innerHTML <BR>var r=/{searchHL}(({(?!\/searchHL})|[^{])*){\/searchHL}/g <BR>t=t.replace(r,"<span class='highlight'>$1"); <BR>obj.innerHTML=t; <BR>alert("搜索到关键词"+cnt+"处") <BR>} <BR>function replace(s,dest){ <BR>var r=new RegExp(s,"g"); <BR>var tm=null; <BR>var t=dest.nodeValue; <BR>var cnt=0; <BR>if (tm=t.match(r)){ <BR>cnt=tm.length; <BR>t=t.replace(r,"{searchHL}"+decode(s)+"{/searchHL}") <BR>dest.nodeValue=t; <BR>} <BR>return cnt; <BR>} <br><br></script> <br> </div> <br><br><br>html部分 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="57903" class="copybut" id="copybut57903" onclick="doCopy('code57903')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code57903"> <br><xml id="DataBinding1" src="datasource.xml"></xml> <br> <br><input name="s" id="s" title="搜索内容:"><input type="submit" value="搜索" onclick="findAll(s.value);return false;"> <br><table width="100%" valign="top" datasrc="#DataBinding1" id="DataT1" border="1" cellpadding="3"> <br><thead> <br><tr> <br><th>ID</th> <br><th>Name</th> <br><th>Desc</th> <br> </tr> <br> </thead> <br><tbody> <br><tr> <br><td><span name="ID" id="ID" datafld="CODE_ID"> </span></td> <br><td><span name="name" id="name" datafld="CODE_NAME"> </span></td> <br><td><span name="desc" id="desc" datafld="CODE_DESC"> </span></td> <br> </tr> <br> </tbody> <br> </table> <br> <br> </div> <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="97423" class="copybut" id="copybut97423" onclick="doCopy('code97423')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code97423"> <br><br><?xml version="1.0" encoding="UTF-8"?> <br><inventory> <br><row> <br><code_id>PCOMM11</code_id> <br><code_name>caracter handle</code_name> <br><code_desc>hkdlhglfghfkgfk</code_desc> <br></row> <br><row> <br><code_id>PCOMM12</code_id> <br><code_name>digital handle</code_name> <br><code_desc>hkdlhglfghfkgfkgggg</code_desc> <br></row> <br></inventory> <br> </div>