首页前端开发JavaScript如何在html页面中实现查找功能

如何在html页面中实现查找功能

时间2024-01-29 08:16:03发布访客分类JavaScript浏览330
导读:收集整理的这篇文章主要介绍了如何在html页面中实现查找功能,觉得挺不错的,现在分享给大家,也给大家做个参考。前台效果:htML<div class="container" style="z-index: 999" id="seArc...
收集整理的这篇文章主要介绍了如何在html页面中实现查找功能,觉得挺不错的,现在分享给大家,也给大家做个参考。

前台效果:

htML

div class="container" style="z-index: 999" id="seArchDiv">
           div class="keyword-search">
               查找:           input id="key" tyPE="text" style="width: 200px;
    " placeholder="关键词" />
               a href="javascript:void(0);
    " class="prev" onclick='wordsearch(1)'>
    i class="c-icon">
    /i>
    /a>
               a href="javascript:void(0);
    " class="next" onclick='wordSearch()'>
    i class="c-icon">
    /i>
    /a>
           /div>
       /div>
    

相关教程推荐:html教程

js

script>
    //搜索功能      VAR oldKey0 = "";
          var index0 = -1;
    var oldCount0 = 0;
          var newflag = 0;
          var currentLength = 0;
      function wordSearch(flg) {
              var key = $("#key").val();
 //取key值          if (!key) {
                  return;
 //key为空则退出          }
              getArray();
              focusNext(flg);
      }
      function focusNext(flg) {
          if (newflag == 0) {
    //如果新搜索,index清零              index0 = 0;
          }
          if (!flg) {
              if (oldCount0 != 0) {
//如果还有搜索                  if (index0  oldCount0) {
    //左边如果没走完,走左边                      focusMove(index0);
                          index0++;
                  }
 else if (index0 == oldCount0) {
    //都走完了                      index0 = 0;
                          focusMove(index0);
                          index0++;
                  }
                  else {
                          index0 = 0;
    //没确定                      focusMove(index0);
                          index0++;
                  }
              }
          }
 else {
              if (oldCount0 != 0) {
    //如果还有搜索                  if (index0 = oldCount0 &
    &
     index0 >
 0) {
    //左边如果没走完,走左边                      index0--;
                          focusMove(index0);
                  }
 else if (index0 == 0) {
    //都走完了                      index0 = oldCount0;
                          index0--                      focusMove(index0);
                  }
              }
          }
      }
      function getArray() {
              newflag = 1;
              $(".contrast .result").removeClass("res");
              var key = $("#key").val();
 //取key值          if (!key) {
                  oldKey0 = "";
                  return;
 //key为空则退出          }
          if (oldKey0 != key || $(".current").length != currentLength) {
                  //重置              index0 = 0;
                  var index = 0;
              $(".contrast .result").each(function () {
                      $(this).replaceWITh($(this).html());
              }
    );
                  pos0 = new Array();
              if ($(".contrast-wrap").hasClass("current")) {
                      currentLength = $(".current").length;
                  $(".current .contrast").each(function () {
                          $(this).html($(this).html().replace(new RegExp(key, "gm"), "span id='result" + (index++) + "' class='result'>
    " + key + "/span>
    "));
 // 替换                  }
    );
              }
 else {
                      $(".contrast-wrap").addClass('current');
                      currentLength = $(".current").length;
                  $(".contrast").each(function () {
                          $(this).html($(this).html().replace(new RegExp(key, "gm"), "span id='result" + (index++) + "' class='result'>
    " + key + "/span>
    "));
 // 替换                  }
    );
              }
                  //$("#key").val(key);
                  oldKey0 = key;
              //$(".contrast .result").each(function () {
                  //    $(this).parents('.contrast-wrap').addClass('current');
                  //    pos0.push($(this).offset().top);
              //}
    );
                  // pos0.push($(".contrast .result:eq(2)").offset().top - $(".contrast .result:eq(2)").parents(".contrast").offset().top);
                  oldCount0 = $(".contrast .result").length;
                  newflag = 0;
          }
      }
      function focusMove(index0) {
              $(".contrast .result:eq(" + index0 + ")").parents('.contrast-wrap').addClass('current');
              $(".contrast .result:eq(" + index0 + ")").addClass("res");
              var top = $(".contrast .result:eq(" + index0 + ")").offset().top + $(".contrast .result:eq(" + index0 + ")").parents(".contrast").scrollTop();
              var intop = top - $(".contrast .result:eq(" + index0 + ")").parents(".contrast").offset().top;
          $(".contrast .result:eq(" + index0 + ")").parents(".contrast").aniMATE({
 scrollTop: intop }
    , 200);
          if ($(".contrast .result:eq(" + index0 + ")").parents(".contrast").scrollTop() == 0) {
              $("html, body").animate({
 scrollTop: top - 200 }
    , 200);
          }
 else {
              $("html, body").animate({
 scrollTop: $(".contrast .result:eq(" + index0 + ")").parents(".contrast").offset().top - 200 }
    , 200);
          }
      }
      $('#key').change(function () {
          if ($('#key').val() == "") {
                  index0 = 0;
              $(".contrast .result").each(function () {
                      $(this).replaceWith($(this).html());
              }
    );
                  oldKey0 = "";
          }
      }
    );
      /script>
    

视频教程推荐:html视频教程

以上就是如何在html页面中实现查找功能的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

上一篇: html如何提高页面的加载速度下一篇:关于html中area标签的用法详解猜你在找的JavaScript相关文章 html font标签如何设置字体大小?html font标签属性用法介绍2022-05-16vue3+TypeScript+vue-router的使用方法2022-04-16vue3获取当前路由地址2022-04-16如何利用React实现图片识别App2022-04-16JavaScript展开运算符和剩余运算符的区别详解2022-04-16微信小程序中使用vant框架的具体步骤2022-04-16Vue elementUI表单嵌套表格并对每行进行校验详解2022-04-16如何利用Typescript封装本地存储2022-04-16微信小程序中wxs文件的一些妙用分享2022-04-16JavaScript的Set数据结构详解2022-04-16 其他相关热搜词更多phpjavapython程序员loadpost-format-gallery

若转载请注明出处: 如何在html页面中实现查找功能
本文地址: https://pptw.com/jishu/590828.html
HTML 之 ContentEditable 属性 关于html中area标签的用法详解

游客 回复需填写必要信息