当前位置: 首页 > 编程笔记 >

jQuery实现搜索页面关键字的功能

柏正平
2023-03-14
本文向大家介绍jQuery实现搜索页面关键字的功能,包括了jQuery实现搜索页面关键字的功能的使用技巧和注意事项,需要的朋友参考一下

在一篇文章中查找关键字,找到后高亮显示。

具体代码:

<html> 
  <head> 
    <title>Search</title> 
    <style type="text/css"> 
      p { border:1px solid black;width:500px;padding:5px;} 
      .highlight { background-color:yellow; } 
    </style> 
  </head> 
  <body> 
    <form> 
      <p> 
        I consider that a man's brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled up with a lot of other things, so that he has a difficulty in laying his hands upon it. 
      </p> 
      <p> 
        I consider that a man's brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled up with a lot of other things, so that he has a difficulty in laying his hands upon it. 
      </p> 
      <p> 
        I consider that a man's brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled up with a lot of other things, so that he has a difficulty in laying his hands upon it. 
      </p> 
      <input type="text" id="text"/> 
      <input type="button" id="search" value="Search"/> 
      <input type="button" id="clear" value="Clear"/> 
    </form> 
<script type="text/javascript" src="../jquery.js"></script> 
<script type="text/javascript"> 
  $(document).ready(function () 
  { 
    $('#search').click(highlight);//点击search时,执行highlight函数; 
    $('#clear').click(clearSelection);//点击clear按钮时,执行clearSelection函数; 
  
    function highlight() 
    { 
      clearSelection();//先清空一下上次高亮显示的内容; 
      var searchText = $('#text').val();//获取你输入的关键字; 
      var regExp = new RegExp(searchText, 'g');//创建正则表达式,g表示全局的,如果不用g,则查找到第一个就不会继续向下查找了; 
      $('p').each(function()//遍历文章; 
      { 
        var html = $(this).html(); 
        var newHtml = html.replace(regExp, '<span class="highlight">'+searchText+'</span>');//将找到的关键字替换,加上highlight属性; 
  
        $(this).html(newHtml);//更新文章; 
      }); 
    } 
    function clearSelection() 
    { 
      $('p').each(function()//遍历 
      { 
        $(this).find('.highlight').each(function()//找到所有highlight属性的元素; 
        { 
          $(this).replaceWith($(this).html());//将他们的属性去掉; 
        }); 
      }); 
    } 
  }); 
   </script> 
  </body> 
</html> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍基于jQuery实现页面搜索功能,包括了基于jQuery实现页面搜索功能的使用技巧和注意事项,需要的朋友参考一下 jQuery实现页面搜索,搜索筛选用户输入的内容! HTML: JQ: 更多搜索功能实现的精彩文章,请点击专题:javascript搜索功能汇总 进行学习 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍基于jQuery实现搜索关键字自动匹配功能,包括了基于jQuery实现搜索关键字自动匹配功能的使用技巧和注意事项,需要的朋友参考一下 今天我们就一起来看一个简单的基于jquery的关键字自动匹配的例子,希望文章能够对各位有帮助。 例子一 在项目中,有时候需要用户选择城市,但是城市太多,用户选择起来不太方便,所以提供了一个用户可以通过输入框输入城市的汉字或者拼音简写。结果示意图如下:

  • 本文向大家介绍jQuery实现动态文字搜索功能,包括了jQuery实现动态文字搜索功能的使用技巧和注意事项,需要的朋友参考一下 先简单讲一下需求:页面中会列出多行个人信息记录,为方便查找,在顶层增加一个搜索栏,可根据用户姓名查找记录。 如果只想查看代码,可跳过分析过程,文章底部提供了完整的代码。 以下是我的编写过程: 动态页面,多条记录均由forEach生成,结构如下: 为了方便调试静态页面,可以

  • 本文向大家介绍angularjs实现分页和搜索功能,包括了angularjs实现分页和搜索功能的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了angularjs实现分页和搜索展示的具体代码,供大家参考,具体内容如下 话不多说,上代码 javascript 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 问题内容: 我是SQL编程的新手。 我有一个表的工作,其中字段是,,,,,,。 我想从前端实施 关键字搜索 。关键字可以位于上表的任何字段中。 这是我尝试过的查询,但包含许多重复的行: 问题答案: 对于VARCHAR字段上的单个关键字,可以使用: 对于描述,通常最好添加全文索引并进行全文搜索(仅适用于MyISAM):

  • 本文向大家介绍vue2实现搜索结果中的搜索关键字高亮的代码,包括了vue2实现搜索结果中的搜索关键字高亮的代码的使用技巧和注意事项,需要的朋友参考一下 具体代码如下所示: 开源项目地址: github.com/alex-0407/v… 效果演示 总结 以上所述是小编给大家介绍的vue2实现搜索结果中的搜索关键字高亮的代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此