Simple implementation of the simulation browser search function using jquery _jquery

Source: Internet
Author: User

Write a hasty, motioned, to be perfected. Copy the following code into a text file and change the file name extension to. html to run.

Copy Code code as follows:

<style type= "Text/css" >
. res
{
color:red;
}
</style>
<script src= "Jquery.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
var oldkey = "";
var index =-1;
var pos = new Array ();
var oldcount = 0;

        function Search (FLG) {
             if (!FLG) {
                 index++;
                index = index = = Oldcount? 0:index;
           }
            else {
                 index--;
                index = Index < 0 ? OldCount-1: Index;
           }

            $ (". Result"). Removeclass ("res");
            $ ("#toresult"). Remove ();
            var key = $ ("#key"). Val ();//Take key value
             if (!key) {
                 Oldkey = "";
                return;// Exit
           }

If key is empty

            if (Oldkey!= key) {
                //Reset
                 index = 0;
                $ (". Result"). each (function () {
                     $ (this). ReplaceWith ($ (this). html ());
               });
                pos = new Array ();

                $ ("body"). HTML ($ ( "Body"). HTML (). Replace (new RegExp (Key, "GM"), "<span id= ' result" + index + "' class= ' result ' >" + key + "</span> ;")); Replace the

$ ("#key"). Val (key);
Oldkey = key;
$ ('. Result '). each (function () {
Pos.push ($ (this). Offset (). top);
});
Oldcount = $ (". Result"). Length;
}

$ (". Result:eq (" + Index + ")"). AddClass ("res");

$ ("Body"). ScrollTop (Pos[index]);
}
</script>
<body>
<div style= "position:fixed; right:20px; top:0; " >
<input id= "Key" type= "text" style= "width:100px;"/>
<input type= "button" value= "Next" onclick= "search ()"/>
<input type= "button" value= "last" onclick= "search (1)"/>
</div>
<div style= "height:50px;" >
</div>
<div style= "height:200px;" >
1 text to search for.
</div>
<div style= "height:200px;" >
2 text to search for.
</div>
<div style= "height:200px;" >
3 Text to search for.
</div>
<div style= "height:200px;" >
4 text to search for.
</div>
<div style= "height:200px;" >
5 text to search for.
</div>
<div style= "height:200px;" >
10 beautiful hometown.
</div>
<div style= "height:200px;" >
11 beautiful hometown.
</div>
<div style= "height:200px;" >
12 beautiful hometown.
</div>
<div style= "height:200px;" >
13 beautiful hometown.
</div>
<div style= "height:200px;" >
14 beautiful hometown.
</div>
<div style= "height:200px;" >
15 beautiful hometown.
</div>
</body>

Here is the main one to provide a train of thought, small partners can not wait to be perfect under their own.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.