使用jQuery簡單實現類比瀏覽器搜尋功能,jquery搜尋功能

來源:互聯網
上載者:User

使用jQuery簡單實現類比瀏覽器搜尋功能,jquery搜尋功能

寫的匆忙,示意一下,有待完善。把以下代碼複製到文字檔中,把副檔名改為.html就可以運行了。

複製代碼 代碼如下:
<html>
<head>
    <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(); //取key值
            if (!key) {
                oldKey = "";
                return; //key為空白則退出
            }

            if (oldKey != key) {
                //重設
                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>")); // 替換

                $("#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>
</head>
<body>
    <div style="position: fixed; right: 20px; top: 0;">
        <input id="key" type="text" style="width: 100px;" />
        <input type="button" value="下一個" onclick="search()" />
        <input type="button" value="上一個" onclick="search(1)" />
    </div>
    <div style="height: 50px;">
    </div>
    <div style="height: 200px;">
        1待搜尋的文本。
    </div>
    <div style="height: 200px;">
        2待搜尋的文本。
    </div>
    <div style="height: 200px;">
        3待搜尋的文本。
    </div>
    <div style="height: 200px;">
        4待搜尋的文本。
    </div>
    <div style="height: 200px;">
        5待搜尋的文本。
    </div>
    <div style="height: 200px;">
        10美麗的家鄉。
    </div>
    <div style="height: 200px;">
        11美麗的家鄉。
    </div>
    <div style="height: 200px;">
        12美麗的家鄉。
    </div>
    <div style="height: 200px;">
        13美麗的家鄉。
    </div>
    <div style="height: 200px;">
        14美麗的家鄉。
    </div>
    <div style="height: 200px;">
        15美麗的家鄉。
    </div>
</body>
</html>

這裡主要是個大家提供一個思路,小夥伴們等不及的話就自己完善下吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.