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使用cookie
Feb 02 Javascript
JS input文本框禁用右键和复制粘贴功能的代码
Apr 15 Javascript
一个分享按钮的插件使用介绍(可扩展,内附开发制作流程)
Sep 19 Javascript
jquery delay()介绍及使用指南
Sep 02 Javascript
jQuery的观察者模式详解
Dec 22 Javascript
JavaScript String 对象常用方法详解
May 13 Javascript
Bootstrap 下拉多选框插件Bootstrap Multiselect
Jan 22 Javascript
AngularJS日程表案例详解
Aug 15 Javascript
node app 打包工具pkg的具体使用
Jan 17 Javascript
Element中的Cascader(级联列表)动态加载省\市\区数据的方法
Mar 27 Javascript
Node.js实现用户评论社区功能(体验前后端开发的乐趣)
May 09 Javascript
node实现socket链接与GPRS进行通信的方法
May 20 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
一个多文件上传的例子(原创)
2006/10/09 PHP
?算你??的 PHP 程式大小
2006/12/06 PHP
PHP 第一节 php简介
2012/04/28 PHP
Yii数据模型中rules类验证器用法分析
2016/07/15 PHP
PHP的RSA加密解密方法以及开发接口使用
2018/02/11 PHP
符合W3C网页标准的iframe标签的使用方法
2007/07/19 Javascript
js获取图片大小的函数代码
2011/09/20 Javascript
基于jquery跨浏览器显示的file上传控件
2011/10/24 Javascript
Javascript数组的排序 sort()方法和reverse()方法
2012/06/04 Javascript
javascript 树形导航菜单实例代码
2013/08/13 Javascript
jquery二级导航内容均分的原理及实现
2013/08/13 Javascript
容易造成JavaScript内存泄露几个方面
2014/09/04 Javascript
一款基jquery超炫的动画导航菜单可响应单击事件
2014/11/02 Javascript
JS实现的打字机效果完整实例
2016/06/20 Javascript
JavaScript实现的微信二维码图片生成器的示例
2016/10/26 Javascript
微信小程序实现YDUI的ScrollTab组件
2018/02/02 Javascript
微信小程序scroll-x失效的完美解决方法
2018/07/18 Javascript
js实现视图和数据双向绑定的方法分析
2020/02/05 Javascript
js抽奖转盘实现方法分析
2020/05/16 Javascript
js实现盒子移动动画效果
2020/08/09 Javascript
ES6学习教程之Promise用法详解
2020/11/22 Javascript
[01:23]一分钟告诉你 DOTA2为什么叫信仰2
2014/06/20 DOTA
用Python写王者荣耀刷金币脚本
2017/12/21 Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
2019/06/27 Python
python字典的遍历3种方法详解
2019/08/10 Python
Django ModelForm操作及验证方式
2020/03/30 Python
如何教少儿学习Python编程
2020/07/10 Python
生产主管岗位职责
2013/11/10 职场文书
项目合作协议书
2014/04/16 职场文书
监察局领导班子四风问题整改措施思想汇报
2014/10/05 职场文书
2014年终个人工作总结
2014/11/07 职场文书
原来闭幕词是这样写的呀!
2019/07/01 职场文书
SQL Server作业失败:无法确定所有者是否有服务器访问权限的解决方法
2021/06/30 SQL Server
关于python中模块和重载的问题
2021/11/02 Python
SpringBoot系列之MongoDB Aggregations用法详解
2022/02/12 MongoDB
Java 定时任务技术趋势简介
2022/05/04 Java/Android