一段javascript的頁面搜尋代碼
來源:互聯網
上載者:User
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();
}
//-->