搜索字符串并高亮显示
<script language="JavaScript">
<!--
function searchText(text){
var count=0,text,dv;
if(text==null || text.length==0) return ;
dv=document.defaultView;
function searchWithinNode(node,te,len){
var pos,skip,spannode,middlebit,endbit,middleclone;
skip=0;
if(node.nodeType==3){
pos=node.data.toUpperCase().indexOf(te);
if(pos>=0){
spannode=document.createElement('SPAN');spannode.style.backgroundColor='yellow';
middlebit=node.splitText(pos);
endbit=middlebit.splitText(len);
middleclone=middlebit.cloneNode(true);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode,middlebit);++count;skip=1;
}
}else if(node.nodeType==1&& node.childNodes && node.tagName.toUpperCase()!='SCRIPT' && node.tagName.toUpperCase!='STYLE'){
for (var child=0;child<node.childNodes.length;++child){
child=child+searchWithinNode(node.childNodes[child],te,len);
}
}
return skip;
};
searchWithinNode(document.body,text.toUpperCase(),text.length);
}
//-->
</script>
<!--
function searchText(text){
var count=0,text,dv;
if(text==null || text.length==0) return ;
dv=document.defaultView;
function searchWithinNode(node,te,len){
var pos,skip,spannode,middlebit,endbit,middleclone;
skip=0;
if(node.nodeType==3){
pos=node.data.toUpperCase().indexOf(te);
if(pos>=0){
spannode=document.createElement('SPAN');spannode.style.backgroundColor='yellow';
middlebit=node.splitText(pos);
endbit=middlebit.splitText(len);
middleclone=middlebit.cloneNode(true);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode,middlebit);++count;skip=1;
}
}else if(node.nodeType==1&& node.childNodes && node.tagName.toUpperCase()!='SCRIPT' && node.tagName.toUpperCase!='STYLE'){
for (var child=0;child<node.childNodes.length;++child){
child=child+searchWithinNode(node.childNodes[child],te,len);
}
}
return skip;
};
searchWithinNode(document.body,text.toUpperCase(),text.length);
}
//-->
</script>