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


Posted in Javascript onFebruary 16, 2017

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

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
保证JavaScript和Asp、Php等后端程序间传值编码统一
Apr 17 Javascript
JavaScript设置FieldSet展开与收缩
May 15 Javascript
JavaScript 空位补零实现代码
Feb 26 Javascript
JS实现刷新父页面不弹出提示框的方法
Jun 22 Javascript
js获取指定字符前/后的字符串简单实例
Oct 27 Javascript
获取JavaScript异步函数的返回值
Dec 21 Javascript
Vue.Draggable实现拖拽效果
Jul 29 Javascript
小程序实现列表删除功能
Oct 30 Javascript
微信头像地址失效踩坑记附带解决方案
Sep 23 Javascript
怎么理解wx.navigateTo的events参数使用详情
May 18 Javascript
vuex 多模块时 模块内部的mutation和action的调用方式
Jul 24 Javascript
jQuery中event.target和this的区别详解
Aug 13 jQuery
canvas时钟效果
Feb 16 #Javascript
支持移动端原生js轮播图
Feb 16 #Javascript
jQuery为DOM动态追加事件的方法
Feb 16 #Javascript
纯js实现html转pdf的简单实例(推荐)
Feb 16 #Javascript
jq给页面添加覆盖层遮罩的实例
Feb 16 #Javascript
基于JavaScript实现全选、不选和反选效果
Feb 15 #Javascript
bootstrap为水平排列的表单和内联表单设置可选的图标
Feb 15 #Javascript
You might like
thinkphp框架实现删除和批量删除
2016/06/29 PHP
Thinkphp实现站点静态化的方法详解
2017/03/21 PHP
JQuery Easyui Tree的oncheck事件实现代码
2010/05/28 Javascript
JQuery自定义事件的应用 JQuery最佳实践
2010/08/01 Javascript
浅析jQuery对select操作小结(遍历option,操作option)
2013/07/04 Javascript
使用JavaScript实现Java的List功能(实例讲解)
2013/11/07 Javascript
页面元素绑定jquery toggle后元素隐藏的解决方法
2014/03/27 Javascript
jQuery基于muipicker实现仿ios时间选择
2016/02/22 Javascript
基于Jquery插件实现跨域异步上传文件功能
2016/04/26 Javascript
AngularJs Javascript MVC 框架
2016/06/20 Javascript
JavaScript箭头(arrow)函数详解
2017/06/04 Javascript
Angular移动端页面input无法输入的解决方法
2017/11/14 Javascript
判断滚动条滑到底部触发事件(实例讲解)
2017/11/15 Javascript
关于layui的动态图标不显示的解决方法
2019/09/04 Javascript
详解React 元素渲染
2020/07/07 Javascript
python爬虫_自动获取seebug的poc实例
2017/08/05 Python
Django中间件工作流程及写法实例代码
2018/02/06 Python
TensorFlow变量管理详解
2018/03/10 Python
Python并发之多进程的方法实例代码
2018/08/15 Python
用python爬取租房网站信息的代码
2018/12/14 Python
Python函数和模块的使用总结
2019/05/20 Python
Python 中使用 PyMySQL模块操作数据库的方法
2019/11/10 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
2020/11/24 Python
世界各地的旅游、观光和活动:Isango!
2019/10/29 全球购物
开门红主持词
2014/04/02 职场文书
广播体操比赛口号
2014/06/10 职场文书
放飞梦想演讲稿800字
2014/08/26 职场文书
汽车机电维修工求职信
2014/09/30 职场文书
民政局离婚协议书范本
2014/10/20 职场文书
计算机专业自荐信
2015/03/05 职场文书
离婚承诺书格式范文
2015/05/04 职场文书
工作服管理制度范本
2015/08/06 职场文书
2016年9月份红领巾广播稿
2015/12/21 职场文书
传单、海报早OUT了,另类传单营销方案送给你!
2019/07/15 职场文书
python 实现两个变量值进行交换的n种操作
2021/06/02 Python
python数据处理之Pandas类型转换
2022/04/28 Python