首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

页面搜索的javascript脚本,该如何处理

2012-02-29 
页面搜索的javascript脚本网上搜索到的页面搜索关键字的脚本大多是这个scriptlanguage JavaScript fu

页面搜索的javascript脚本
网上搜索到的页面搜索关键字的脚本大多是这个

<script       language= "JavaScript ">      
       
    function       findInPage(str)          
    {          
        var       txt,       i,       found,n       =       0;          
        if       (str       ==       " ")          
        {          
            return       false;          
        }          
        txt       =       document.body.createTextRange();          
        for       (i       =       0;       i       <=       n       &&       (found       =       txt.findText(str))       !=       false;       i++)          
        {          
            txt.moveStart( "character ",       1);          
            txt.moveEnd( "textedit ");          
        }          
        if       (found)          
        {          
            txt.moveStart( "character ",       -1);          
            txt.findText(str);          
            txt.select();          
            txt.scrollIntoView();          
            n++;                  
        }          
        else          
        {          
            if       (n       >       0)          
            {          
                n       =       0;          
                findInPage(str);          
            }          
            else          
            {          
                alert(str       +       "---您要找的文字不存在。\n       \n请试着输入页面中的关键字再次查找! ");          


            }          
        }          
        return       false;          
    }      
       
    function       find_str()      
    {      
    if       (document.all.find_content.value== " ")      
    {      
    alert( "请先输入你要查找的内容 ");      
    }      
    else      
    {      
    findInPage(document.all.find_content.value);      
    }      
    }      
       
    </script>      

<input       type= "text "       id= "find_content ">      
<input       type= "button "       name= "Submit "       value= "搜索 "       onClick= "find_str() ">      


但这个脚本如果碰到多个相同的关键词则只能定位到第一个,如何改成像IE页面搜索一样能定位到下一个的效果?

[解决办法]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<html>
<head>
<title> New Document </title>
<meta name= "Generator " content= "EditPlus ">
<meta name= "Author " content= " ">
<meta name= "Keywords " content= " ">
<meta name= "Description " content= " ">
</head>

<body>
<script language= "JavaScript ">
var NS4 = (document.layers);
var IE4 = (document.all);
var win = window;
var n = 0;
function findInPage(str) {
var txt, i, found;
if (str == " ")
return false;
if (NS4) {
if (!win.find(str))
while(win.find(str, false, true))
n++;
else
n++;
if (n == 0)
alert( "没有找到。 ");
}
if (IE4) {
txt = win.document.body.createTextRange();
for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
txt.moveStart( "character ", 1);
txt.moveEnd( "textedit ");
}
if (found) {
txt.moveStart( "character ", -1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n++;
}
else {
if (n > 0) {
n = 0;
findInPage(str);
}
else
alert( "没有找到。 ");
}
}
return false;
}
</script>
<form name= "search " onSubmit= "return findInPage(this.string.value); ">
<font size=3> <input name= "string " type= "text " size=15 onChange= "n = 0; "> </font>


<input type= "submit " value= "搜索 "> 查找页面关键字 asdfasfda
</form>
</body>
</html>

热点排行