国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

使用jQuery簡單實現模擬瀏覽器搜索功能

2019-11-20 13:39:01
字體:
來源:轉載
供稿:網友

寫的匆忙,示意一下,有待完善。把以下代碼復制到文本文件中,把文件擴展名改為.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>

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洮南市| 乐至县| 石阡县| 木兰县| 枣强县| 鲁甸县| 永顺县| 上饶市| 互助| 永福县| 湾仔区| 搜索| 黑龙江省| 普陀区| 嘉鱼县| 崇阳县| 佳木斯市| 抚州市| 萍乡市| 石嘴山市| 祁阳县| 孝昌县| 聂荣县| 岳普湖县| 宕昌县| 尤溪县| 渭源县| 太仓市| 德保县| 天峨县| 淅川县| 建水县| 玛曲县| 呼和浩特市| 屏边| 青海省| 松阳县| 梁山县| 石河子市| 武强县| 石林|