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

首頁 > 編程 > HTML > 正文

如何在html頁面中實現查找功能

2020-03-24 13:52:55
字體:
來源:轉載
供稿:網友

前臺效果:

1c47607531c598617a5e8004471ebc1.png

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頁面中實現查找功能的詳細內容,更多請關注電腦知識其它相關文章!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊金霍洛旗| 额敏县| 乳源| 辽宁省| 太原市| 共和县| 甘谷县| 冷水江市| 都昌县| 蒙自县| 班戈县| 云霄县| 遂溪县| 曲沃县| 桂阳县| 通许县| 新田县| 东光县| 若尔盖县| 平顶山市| 三明市| 大渡口区| 孝感市| 安岳县| 屏东县| 太和县| 噶尔县| 于都县| 沈丘县| 扶绥县| 肥城市| 莱阳市| 奉贤区| 上饶市| 建湖县| 利川市| 资阳市| 冷水江市| 平乐县| 南郑县| 偏关县|