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 相关文章推荐
JS实现图片翻书效果示例代码
Sep 09 Javascript
jquery插件NProgress.js制作网页加载进度条
Jun 05 Javascript
利用python分析access日志的方法
Oct 26 Javascript
jQuery学习之DOM节点的插入方法总结
Jan 22 Javascript
bootstrap fileinput组件整合Springmvc上传图片到本地磁盘
May 11 Javascript
node.js操作mongodb简单示例分享
May 25 Javascript
利用vueJs实现图片轮播实例代码
Jun 03 Javascript
Node.js自定义实现文件路由功能
Sep 22 Javascript
JS设计模式之单例模式(一)
Sep 29 Javascript
JS非行间样式获取函数的实例代码
Jun 05 Javascript
vue2.0+SVG实现音乐播放圆形进度条组件
Sep 21 Javascript
vue-cli3自动消除console.log()的调试信息方式
Oct 21 Javascript
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
php 显示指定路径下的图片
2009/10/29 PHP
php中一个有意思的日期逻辑处理
2012/03/25 PHP
克隆一个新项目的快捷方式
2013/04/10 PHP
[原创]php token使用与验证示例【测试可用】
2017/08/30 PHP
php mysql PDO 查询操作的实例详解
2017/09/23 PHP
PHP单元测试框架PHPUnit用法详解
2019/01/23 PHP
Yii2框架加载css和js文件的方法分析
2019/05/25 PHP
用JavaScrpt实现文件夹简单轻松加密的实现方法图文
2008/09/08 Javascript
JavaScript单元测试ABC
2012/04/12 Javascript
Jquery选择子控件&quot;大于号&quot;和&quot; &quot;区别介绍及使用示例
2013/06/25 Javascript
jquery实现动态画圆
2014/12/04 Javascript
Grunt入门教程(自动任务运行器)
2015/08/06 Javascript
js实现点击文本框显示日期选择器特效代码分享
2020/05/21 Javascript
jquery事件的ready()方法使用详解
2015/11/11 Javascript
Bootstrap每天必学之js插件
2015/11/30 Javascript
jQuery实现摸拟alert提示框
2016/05/22 Javascript
详解用函数式编程对JavaScript进行断舍离
2017/09/18 Javascript
vue项目从node8.x升级到12.x后的问题解决
2019/10/25 Javascript
微信小程序wx.request的简单封装
2019/11/13 Javascript
js操作两个json数组合并、去重,以及删除某一项元素
2020/09/22 Javascript
nginx配置域名后的二级目录访问不同项目的配置操作
2020/11/06 Javascript
基于python3 pyQt5 QtDesignner实现窗口化猜数字游戏功能
2019/07/15 Python
Python函数中的可变长参数详解
2019/09/12 Python
python 实现多线程下载视频的代码
2019/11/15 Python
python cv2读取rtsp实时码流按时生成连续视频文件方式
2019/12/25 Python
Python实现i人事自动打卡的示例代码
2020/01/09 Python
python logging模块的使用
2020/09/07 Python
python输出国际象棋棋盘的实例分享
2020/11/26 Python
什么是makefile? 如何编写makefile?
2013/01/02 面试题
大四学生毕业自荐信
2013/11/07 职场文书
文秘专业毕业生就业推荐信
2013/11/08 职场文书
人事专员的岗位职责
2014/03/01 职场文书
团队队名口号大全
2014/06/06 职场文书
护理培训心得体会
2016/01/22 职场文书
python中的None与NULL用法说明
2021/05/25 Python
十大最强水系宝可梦,最美宝可梦排第三,榜首大家最熟悉
2022/03/18 日漫