Home  >  Article  >  Web Front-end  >  How to make search function in html page

How to make search function in html page

php中世界最好的语言
php中世界最好的语言Original
2018-01-16 09:13:567164browse

This time I will show you how to make a search function in an html page. How to make a search function in an html page? What are the precautions for making the search function in the HTML page? The following is a practical case, let's take a look.

Recently I am working on a framework that has been modified by many people. I feel dizzy looking at the code every day, but I feel that the progress is quite big. I have made a backend and configurable frontend to view different data in the two libraries. I am quite satisfied with the range of things. I took it out to share it that day. Today I will talk about a function that I have been working on in the past few days, which is the search function of html pages.

This function is mainly implemented by entering the characters in the search box, and then pressing the previous and next buttons, which will automatically mark the matching characters in the query area with a special style. You can continue to press the previous and next buttons to browse the matching characters in order, and use another style to distinguish the currently matching characters from other matching characters.

<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=&#39;wordSearch(1)&#39;><i class="c-icon"></i></a>
           <a href="javascript:void(0);" class="next" onclick=&#39;wordSearch()&#39;><i class="c-icon"></i></a>
       </div>
   </div>
<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=&#39;result" + (index++) + "&#39; class=&#39;result&#39;>" + key + "</span>")); // 替换
                  });
              } else {
                  $(".contrast-wrap").addClass(&#39;current&#39;);
                  currentLength = $(".current").length;
                  $(".contrast").each(function () {
                      $(this).html($(this).html().replace(new RegExp(key, "gm"), "<span id=&#39;result" + (index++) + "&#39; class=&#39;result&#39;>" + key + "</span>")); // 替换
                  });
              }
              //$("#key").val(key);
              oldKey0 = key;
              //$(".contrast .result").each(function () {
              //    $(this).parents(&#39;.contrast-wrap&#39;).addClass(&#39;current&#39;);
              //    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(&#39;.contrast-wrap&#39;).addClass(&#39;current&#39;);
          $(".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);
          }
      }
      $(&#39;#key&#39;).change(function () {
          if ($(&#39;#key&#39;).val() == "") {
              index0 = 0;
              $(".contrast .result").each(function () {
                  $(this).replaceWith($(this).html());
              });
              oldKey0 = "";
          }
      });
  </script>

Next, remember the implementation principle:

First clear the last query results, and then use regular expressions All matching characters are added with a special style. For example, a span tag with a class name of result is added to the method, and the odKey0 variable is used to record the value of the key (compare first when entering next time. If they are the same, it means that you need to look at the next one. Or the previous content, there is no need to use regular expression matching when entering), oldCount0 records the total number of queries, and newflag is set to 0 (if it is not the first query, newflag is 1).

Then enter the getNext method. flg indicates whether the user pressed the previous or next button. Use index0 to record which matching character is currently viewed. Compare it with oldCount0 to determine whether it is incrementing or decrementing or setting to 0 ( If it is greater than oldCount0 or less than 0, it will start again).

The focusMove method is an operation that positions the page to the current element.

JQuery methods learned:

eq() Selector: The selector selects elements with a specified index value. For example: $(".contrast .result:eq(1)") selects the second element whose class name is result among the class name contrast elements.

parents() method: all parent elements of the element. $("p").parents('.contrast-wrap'), all elements named contrast-wrap of the p element.

replace() method: Replace the selected element with the specified html content. Note that the elements of the selected element are also replaced.

offset() method: Returns or sets the offset (position) of the matching element relative to the document.

scrollTop() method: Returns or sets the vertical position of the scroll bar of the matching element.

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

How to solve the gap in the picture in H5

How to make the calendar verification function in H5

How to call the camera to take pictures and compress pictures in H5


The above is the detailed content of How to make search function in html page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn