模拟浏览器搜索功能(Ctrl + F)

写的匆忙,示意一下,有待完善。把以下代码复制到文本文件中,把文件扩展名改为.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>

 

转载于:https://www.cnblogs.com/s0611163/p/3971049.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值