• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    html页面中完成查找功能

    发布者: 颖然780 | 发布时间: 2025-8-13 00:51| 查看数: 18| 评论数: 0|帖子模式

    最近在搞一个被很多人改了的框架,天天看代码看的头的晕了,不过感觉进步还挺大的,自己做了一个后台可配置前台查看两个库不同数据范围的东西,还挺满意,那天拿出来分享一下,今天先说一个这几天做的功能,就是html页面的查找功能。
    这个功能主要是实现在查找框内输入字符,之后按后面的上一个下一个按钮,会自动把查询区域内的匹配字符用特殊的样式标记,之后可以继续按上一个下一个按钮把按照顺序浏览匹配字符,并把当前匹配的字符用另一种样式与其他匹配字符加以区别。
    前台显示大概是这个样子:

    html是这样:
    1. <div class="container" style="z-index: 999" id="searchDiv">
    2.         <div class="keyword-search">
    3.             查找:
    4.             <input id="key" type="text" style="width: 200px;" placeholder="关键词" />
    5.             <a href="javascript:void(0);" class="prev" onclick='wordSearch(1)'><i class="c-icon"></i></a>
    6.             <a href="javascript:void(0);" class="next" onclick='wordSearch()'><i class="c-icon"></i></a>
    7.         </div>
    8.     </div>
    复制代码
    script代码:
    1.   <script>//搜索功能
    2.         var oldKey0 = "";
    3.         var index0 = -1;var oldCount0 = 0;
    4.         var newflag = 0;
    5.         var currentLength = 0;
    6.         function wordSearch(flg) {
    7.             var key = $("#key").val(); //取key值
    8.             if (!key) {
    9.                 return; //key为空则退出
    10.             }
    11.             getArray();
    12.             focusNext(flg);
    13.         }
    14.         function focusNext(flg) {
    15.             if (newflag == 0) {//如果新搜索,index清零
    16.                 index0 = 0;
    17.             }
    18.             if (!flg) {
    19.                 if (oldCount0 != 0) {//如果还有搜索
    20.                     if (index0 < oldCount0) {//左边如果没走完,走左边
    21.                         focusMove(index0);
    22.                         index0++;
    23.                     } else if (index0 == oldCount0) {//都走完了
    24.                         index0 = 0;
    25.                         focusMove(index0);
    26.                         index0++;
    27.                     }
    28.                     else {
    29.                         index0 = 0;//没确定
    30.                         focusMove(index0);
    31.                         index0++;
    32.                     }
    33.                 }
    34.             } else {
    35.                 if (oldCount0 != 0) {//如果还有搜索
    36.                     if (index0 <= oldCount0 && index0 > 0) {//左边如果没走完,走左边
    37.                         index0--;
    38.                         focusMove(index0);
    39.                     } else if (index0 == 0) {//都走完了
    40.                         index0 = oldCount0;
    41.                         index0--
    42.                         focusMove(index0);
    43.                     }
    44.                 }
    45.             }
    46.         }
    47.         function getArray() {
    48.             newflag = 1;
    49.             $(".contrast .result").removeClass("res");
    50.             var key = $("#key").val(); //取key值
    51.             if (!key) {
    52.                 oldKey0 = "";
    53.                 return; //key为空则退出
    54.             }
    55.             if (oldKey0 != key || $(".current").length != currentLength) {
    56.                 //重置
    57.                 index0 = 0;
    58.                 var index = 0;
    59.                 $(".contrast .result").each(function () {
    60.                     $(this).replaceWith($(this).html());
    61.                 });
    62.                 pos0 = new Array();
    63.                 if ($(".contrast-wrap").hasClass("current")) {
    64.                     currentLength = $(".current").length;
    65.                     $(".current .contrast").each(function () {
    66.                         $(this).html($(this).html().replace(new RegExp(key, "gm"), "<span id='result" + (index++) + "' class='result'>" + key + "</span>")); // 替换
    67.                     });
    68.                 } else {
    69.                     $(".contrast-wrap").addClass('current');
    70.                     currentLength = $(".current").length;
    71.                     $(".contrast").each(function () {
    72.                         $(this).html($(this).html().replace(new RegExp(key, "gm"), "<span id='result" + (index++) + "' class='result'>" + key + "</span>")); // 替换
    73.                     });
    74.                 }
    75.                 //$("#key").val(key);
    76.                 oldKey0 = key;
    77.                 //$(".contrast .result").each(function () {
    78.                 //    $(this).parents('.contrast-wrap').addClass('current');
    79.                 //    pos0.push($(this).offset().top);
    80.                 //});
    81.                 // pos0.push($(".contrast .result:eq(2)").offset().top - $(".contrast .result:eq(2)").parents(".contrast").offset().top);
    82.                 oldCount0 = $(".contrast .result").length;
    83.                 newflag = 0;
    84.             }
    85.         }
    86.         function focusMove(index0) {
    87.             $(".contrast .result:eq(" + index0 + ")").parents('.contrast-wrap').addClass('current');
    88.             $(".contrast .result:eq(" + index0 + ")").addClass("res");
    89.             var top = $(".contrast .result:eq(" + index0 + ")").offset().top + $(".contrast .result:eq(" + index0 + ")").parents(".contrast").scrollTop();
    90.             var intop = top - $(".contrast .result:eq(" + index0 + ")").parents(".contrast").offset().top;
    91.             $(".contrast .result:eq(" + index0 + ")").parents(".contrast").animate({ scrollTop: intop }, 200);
    92.             if ($(".contrast .result:eq(" + index0 + ")").parents(".contrast").scrollTop() == 0) {
    93.                 $("html, body").animate({ scrollTop: top - 200 }, 200);
    94.             } else {
    95.                 $("html, body").animate({ scrollTop: $(".contrast .result:eq(" + index0 + ")").parents(".contrast").offset().top - 200 }, 200);
    96.             }
    97.         }
    98.         $('#key').change(function () {
    99.             if ($('#key').val() == "") {
    100.                 index0 = 0;
    101.                 $(".contrast .result").each(function () {
    102.                     $(this).replaceWith($(this).html());
    103.                 });
    104.                 oldKey0 = "";
    105.             }
    106.         });
    107.     </script>
    复制代码
    接下来记一下实现原理:
    首先先把上一次的查询结果清除掉,然后根据key的值,用正则表达式把区域内所有匹配的字符全都加上特殊的样式,比如方法中就全部加了一个类名为result的span标签,用odKey0变量记录key的值(下次再进入先比较如果一样的话说明是要看下一个或者上一个的内容,就不用在进入用正则表达式匹配了),oldCount0记录总共查询出来的个数,newflag置0(如果不是初次查询newflag为1)。
    接着进入getNext方法,flg表示用户按下的是上一个还是下一个按钮,用index0记录当前查看的是哪一个匹配字符,与oldCount0比较,确定是递增或递减还是置0(如果大于oldCount0或者小于0,就要重新开始)。
    focusMove方法就是使页面定位到当前元素的操作。
    学到的jquery方法:
    eq() 选择器:选择器选取带有指定 index 值的元素。例如:
    1. $(".contrast .result:eq(1)")
    复制代码
    ,就是选择类名contrast元素中的第二个类名为result的元素。
    parents()方法:元素的所有父元素。
    1. $("p").parents('.contrast-wrap')
    复制代码
    ,p元素所有类名为contrast-wrap的元素。
    replace()方法:用指定的html内容替换被选元素,注意是把被选元素的元素也替换掉。
    offset()方法:返回或设置匹配元素相对于文档的偏移(位置)。
    scrollTop()方法:返回或设置匹配元素的滚动条的垂直位置。
    总结
    以上所述是小编给大家介绍的html页面中完成查找功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表