开发者

Search HTML and ScrollTo

开发者 https://www.devze.com 2023-01-27 05:04 出处:网络
So I\'ve got a list a document with 2000 plus entries. I want to add some form of search functionality. I have an input bar, obviously, but how would I go about writing the javascript that searches th

So I've got a list a document with 2000 plus entries. I want to add some form of search functionality. I have an input bar, obviously, but how would I go about writing the javascript that searches the page and the 开发者_如何学CScrollTo's to that location?


You can use window.find and range.findText

<input id="search" >
<input type="button" value="search" onclick="fx(document.getElementById('search'))">
<script type="text/javascript">
<!--
function fx(o)
{ 
  var _o=o;
  if(window.find)
  { 
    _o.style.visibility='hidden';
    setTimeout(function(){window.find(_o.value);_o.style.visibility='visible';},10);
  }
  else if(document.body.createTextRange)
  {
    var rng=document.body.createTextRange();
        rng.findText(o.value);
        rng.select();
  }
}
//-->
</script>


Loop through elements you want to search. Step recursively through child nodes and search each one with a regex, probably something like /\bTERM\b/.

When you find one, locate its offset position, and then scrollTo() that offset.

0

精彩评论

暂无评论...
验证码 换一张
取 消