一段javascript的页面搜索代码

80酷酷网    80kuku.com

  javascript|页面/*
搜索文字核心程序,仿CRL+F,但功能更强的搜索
*/
<!--
var iFlag;
function search() {
var wPopupElements=wPopup.document.all;
if (wPopupElements.textcase.checked)
  {iFlag=4;}
else
  {iFlag=0;}
if (wPopupElements.searchtext.value==null || wPopupElements.searchtext.value=='')
  {
   wPopup.alert("请输入你要查找的文字");
   return;
  }
if (rng.findText(wPopupElements.searchtext.value,10000,iFlag)==true)
  {
   rng.select();
   rng.scrollIntoView();
   rng.moveStart("character");
  }
else
  {wPopup.alert("已查找到文档底部");}
}

function replace() {
var wPopupElements=wPopup.document.all;
if (wPopupElements.textcase.checked)
  {iFlag=4;}
else
  {iFlag=0;}
if (wPopupElements.searchtext.value==null || wPopupElements.searchtext.value=='')
  {
   wPopup.alert("请输入你要替换的文字");
   return;
  }
if (rng.findText(wPopupElements.searchtext.value,-10000,iFlag)==true)
  {
   rng.text = wPopupElements.replacetext.value;
  }
else
  {wPopup.alert("已替换到文档底部");}
}

function replaceall() {
var rng = document.body.createTextRange();
var wPopupElements=wPopup.document.all;
if (wPopupElements.textcase.checked)
  {iFlag=4;}
else
  {iFlag=0;}
if (wPopupElements.searchtext.value==null || wPopupElements.searchtext.value=='')
  {
   wPopup.alert("请输入你要替换的文字");
   return;
  }
  for (i=0; rng.findText(wPopupElements.searchtext.value,10000,iFlag)!=false; i++)
  {
   rng.scrollIntoView();
   rng.text = wPopupElements.replacetext.value;
  }
  setTimeout('wPopup.alert("共有"+i + " 项被替换")',200);
}
//-->


/*
弹出搜索窗口程序
*/
<!--
function gofind() {
wPopup=window.open('about:blank','','width=300 height=108 left=350 top=200 menubar=no resizeable=no scrollbars=no toolbar=no');
var wPopupDoc=wPopup.document;
wPopupDoc.open();
wPopupDoc.write('<html>\n');
wPopupDoc.write('<head>\n');
wPopupDoc.write('<title>文字搜索功能</title>\n');
wPopupDoc.write('<style>\n');
wPopupDoc.write('* {font-size:11px;font-family:"Tahoma","Verdana","Arial"}\n');
wPopupDoc.write('button {width:80}\n');
wPopupDoc.write('</style>\n');
wPopupDoc.write('</head>\n');
wPopupDoc.write('<body bgcolor="buttonface" leftmargin="0" topmargin="0">\n');
wPopupDoc.write('<table width="300" height="108" border="0" cellspacing="0" cellpadding="3" bgcolor="#F1F2DF">\n');
wPopupDoc.write('<tr>\n');
wPopupDoc.write('<td>查找:</td>\n');
wPopupDoc.write('<td><input type="text" name="searchtext" ></td>\n');
wPopupDoc.write('<td><button accesskey="S" name="searchbutton" >查找</button></td>\n');
wPopupDoc.write('</tr>\n');
wPopupDoc.write('<tr>\n');
wPopupDoc.write('<td>替换:</td>\n');
wPopupDoc.write('<td><input type="text" name="replacetext"></td>\n');
wPopupDoc.write('<td><button accesskey="R" name="replacebutton" >替换</button></td>\n');
wPopupDoc.write('</tr>\n');
wPopupDoc.write('<tr>\n');
wPopupDoc.write('<td colspan="2"><input type="checkbox" name="textcase" value="textcase">区分大小写</td>\n');
wPopupDoc.write('<td><button accesskey="A" name="replaceallbutton" >替换全部</button></td>\n');
wPopupDoc.write('</tr>\n');
wPopupDoc.write('<tr>\n');
wPopupDoc.write('<td colspan="2"> </td>\n');
wPopupDoc.write('<td><button accesskey="C" name="cancelbutton" >关闭</button></td>\n');
wPopupDoc.write('</tr>\n');
wPopupDoc.write('</table>\n');
wPopupDoc.write('</body>\n');
wPopupDoc.write('</html>\n');
wPopupDoc.close();
}
//-->


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: