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实现一个页面多个css样式实现
May 29 Javascript
jQuery 树形结构的选择器
Feb 15 Javascript
Jquery公告滚动+AJAX后台得到数据
Apr 14 Javascript
jquery中通过父级查找进行定位示例
Jun 28 Javascript
php的文件上传入门教程(实例讲解)
Apr 10 Javascript
基于AngularJs + Bootstrap + AngularStrap相结合实现省市区联动代码
May 30 Javascript
微信开发 微信授权详解
Oct 21 Javascript
jQuery插件FusionWidgets实现的AngularGauge图效果示例【附demo源码】
Mar 23 jQuery
JavaScript使用链式方法封装jQuery中CSS()方法示例
Apr 07 jQuery
详解Vue取消eslint语法限制
Aug 04 Javascript
JS实现动态无缝轮播
Jan 11 Javascript
AJAX检测用户名是否存在的方法
Mar 24 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下使用strpos需要注意 === 运算符
2010/07/17 PHP
TMDPHP 模板引擎使用教程
2012/03/13 PHP
用PHP实现 上一篇、下一篇的代码
2012/09/29 PHP
php的memcache类分享(memcache队列)
2014/03/26 PHP
PHP实现加密的几种方式介绍
2015/02/22 PHP
php在数组中查找指定值的方法
2015/03/17 PHP
php常用表单验证类用法实例
2015/06/18 PHP
Yii2 rbac权限控制操作步骤实例教程
2016/04/29 PHP
thinkphp实现把数据库中的列的值存到下拉框中的方法
2017/01/20 PHP
PHP实现会员账号单唯一登录的方法分析
2019/03/07 PHP
通过隐藏option实现select的联动效果
2009/11/10 Javascript
基于javascipt-dom编程 table对象的使用
2013/04/22 Javascript
jquery定时滑出可最小化的底部提示层特效代码
2013/10/02 Javascript
node.js中的querystring.escape方法使用说明
2014/12/10 Javascript
使用Plupload实现直接上传附件至七牛云存储
2014/12/26 Javascript
javascript实现当前页导航激活的方法
2015/02/27 Javascript
在JavaScript中访问字符串的子串
2015/07/07 Javascript
js判断手机浏览器操作系统和微信浏览器的方法
2016/04/30 Javascript
jQuery 自定义下拉框(DropDown)附源码下载
2016/07/22 Javascript
jQuery通过ajax方法获取json数据不执行success的原因及解决方法
2016/10/15 Javascript
JS针对Array的各种操作汇总
2016/11/29 Javascript
jquery实现弹窗功能(窗口居中显示)
2017/02/27 Javascript
详解nodejs微信公众号开发——1.接入微信公众号
2017/04/10 NodeJs
原生js检测页面加载完毕的实例
2018/09/11 Javascript
js实现点击展开隐藏效果(实例代码)
2018/09/28 Javascript
js实现移动端轮播图
2020/12/21 Javascript
vue递归获取父元素的元素实例
2020/08/07 Javascript
用Python输出一个杨辉三角的例子
2014/06/13 Python
Python实现全排列的打印
2018/08/18 Python
通过python改变图片特定区域的颜色详解
2019/07/15 Python
python 图像的离散傅立叶变换实例
2020/01/02 Python
KIKO MILANO英国官网:意大利知名化妆品和护肤品品牌
2017/09/25 全球购物
Skyscanner加拿大:全球旅行搜索平台
2018/11/19 全球购物
人身损害赔偿协议书范本
2014/09/27 职场文书
Python 批量下载阴阳师网站壁纸
2021/05/19 Python
SQL Server作业失败:无法确定所有者是否有服务器访问权限的解决方法
2021/06/30 SQL Server