当前位置: 代码迷 >> Java Web开发 >> 怎的搜索当前页面,找到关键字后,将页面定位在此关键字
  详细解决方案

怎的搜索当前页面,找到关键字后,将页面定位在此关键字

热度:225   发布时间:2016-04-13 22:43:48.0
怎样搜索当前页面,找到关键字后,将页面定位在此关键字?
搜索当前页面,找到关键字后,将页面定位在此关键字
window.scrollTo(120, 1500);//
------解决思路----------------------
ie解决方案:
//查询相关内容
var objFindStr = (document.all); 
// window to search. 
var winFindStr = window; 
var nFindStr = 0;
var keepStr = '';
function findInPage(str){
if(!str 
------解决思路----------------------
 str == "") return false;
str = str.replace(/[\s]+/g,'');
//此处代码确保更换新的查询字符时,查询到的坐标保持在第一位
if(keepStr==''
------解决思路----------------------
keepStr!=str){
nFindStr = 0;
keepStr = str;
}
var txt, i, found;
try{
txt = winFindStr.document.body.createTextRange();
}catch(e){
return false;
}
var foundFlag = GetInfoFromTable(str);
if (foundFlag) {
for (i = 0; i <= nFindStr && (found = txt.findText(str)) != false; i++) {
txt.moveStart("character", 1);
txt.moveEnd("textedit");
}
if (found) {
txt.moveStart("character", -1);
txt.findText(str);
try{
txt.select();
txt.scrollIntoView();
nFindStr++;
}catch(e){
nFindStr++;
findInPage(str);
}
} else {
if (nFindStr > 0) {
nFindStr = 0;
findInPage(str);
} else
alert("对不起!没有你要找的内容。");
}
}else{
alert("对不起!没有你要找的内容。");
}
}

function GetInfoFromTable(keyWord) {
var foundFlag = false;
var tableObj = Ext.query("table[class=extable]")[0];//查询区域,也可以使用Document直接获得
//foundFlag = tableObj.innerText.indexOf(keyWord)>-1;
if(tableObj){
for (var i = 0; i < tableObj.rows.length; i++) {    //遍历Table的所有Row
for (var j = 0; j < tableObj.rows[i].cells.length; j++) {   //遍历Row中的每一列
if(tableObj.rows[i].cells[j].innerText!=''){
if(tableObj.rows[i].cells[j].innerText.indexOf(keyWord)>-1){foundFlag=true;}
var newText = search_do(tableObj.rows[i].cells[j],keyWord);
//tableObj.rows[i].cells[j].style.backgroundColor = '';
if(newText!=tableObj.rows[i].cells[j].innerText){
tableObj.rows[i].cells[j].innerHTML = newText;
//tableObj.rows[i].cells[j].style.backgroundColor = 'grey';
}
}
}
}
}
return foundFlag;
}

function search_do(cell,keyWord){
var content = cell.innerHTML;
if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE8.0"){
if(content.indexOf('<SPAN style=\"COLOR: blue\">') >= 0 ){
content = content.replace('<SPAN style=\"COLOR: blue\">','');
content = content.replace('</SPAN>','');
cell.innerHTML = content;
}
}else{
if(content.indexOf('<span style=\"color: blue;\">') >= 0 ){
content = content.replace('<span style=\"color: blue;\">','');
content = content.replace('</span>','');
cell.innerHTML = content;
}
}
if(content.indexOf('<SPAN style=\"COLOR: blue\">') >= 0 ){
content = content.replace('<SPAN style=\"COLOR: blue\">','');
content = content.replace('</SPAN>','');
cell.innerHTML = content;
}

var contentText = cell.innerText;
if(content.indexOf('input') >= 0
------解决思路----------------------
content.indexOf('INPUT') >= 0){
var inputTextStart = content.substring(0,content.indexOf(contentText));
var inputTextEnd = content.substring(content.indexOf(contentText)+contentText.length,content.length);
}
//放弃使用正则替换,正则替换时当关键字包含.    会造成匹配混乱
//此处采用大小写混用,完全是因为万恶的IE8会自动把span和color转成大写并导致无法替换上次查询执行的改色, 〒_〒,另外包括blue后边的;也被IE8和谐
contentText = contentText.replace(keyWord,'<SPAN style="COLOR:blue">'+keyWord+'</SPAN>');
if(inputTextStart){
contentText = inputTextStart+contentText+inputTextEnd;
}
return contentText;
}
//查询相关内容结束
  相关解决方案