您的位置:首页 > Web前端 > JavaScript

一段javascript的页面搜索代码

2007-04-26 10:04 323 查看
/*
搜索文字核心程序,仿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" onchange="window.opener.rng=window.opener.document.body.createTextRange()"></td>/n');
wPopupDoc.write('<td><button accesskey="S" name="searchbutton" onclick="window.opener.search()">查找</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" onclick="window.opener.replace()">替换</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" onclick="window.opener.replaceall()">替换全部</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" onclick="window.close()">关闭</button></td>/n');
wPopupDoc.write('</tr>/n');
wPopupDoc.write('</table>/n');
wPopupDoc.write('</body>/n');
wPopupDoc.write('</html>/n');
wPopupDoc.close();
}
//-->
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: