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 相关文章推荐
让iframe框架网页在任何浏览器下自动伸缩
Aug 18 Javascript
javascript写的日历类(基于pj)
Dec 28 Javascript
jquery radio 操作代码
Mar 16 Javascript
Package.js  现代化的JavaScript项目make工具
May 23 Javascript
javascript中hasOwnProperty() 方法使用指南
Mar 09 Javascript
jQuery原生的动画效果
Jul 10 Javascript
AngulerJS学习之按需动态加载文件
Feb 13 Javascript
javascript 中的继承实例详解
May 05 Javascript
BootStrap导航栏问题记录
Jul 31 Javascript
Vue实现按钮级权限方案
Nov 21 Javascript
详解JS深拷贝与浅拷贝
Aug 04 Javascript
JavaScript选择器函数querySelector和querySelectorAll
Nov 27 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中运用http调用的GET和POST方法示例
2014/09/29 PHP
Linux系统中为php添加pcntl扩展
2016/08/28 PHP
PHP中include和require的区别实例分析
2017/05/07 PHP
Laravel学习教程之request validation的编写
2017/10/25 PHP
swoole和websocket简单聊天室开发
2017/11/18 PHP
PHP预定义超全局数组变量小结
2018/08/20 PHP
php实现JWT(json web token)鉴权实例详解
2019/11/05 PHP
laravel框架中间件简单使用方法示例
2020/01/25 PHP
DOMAssitant最新版 DOMAssistant 2.5发布
2007/12/25 Javascript
js 获取浏览器版本以此来调整CSS的样式
2014/06/03 Javascript
jquery中trigger()无法触发hover事件的解决方法
2015/05/07 Javascript
javascript类型系统 Window对象学习笔记
2016/01/07 Javascript
JavaScript使用链式方法封装jQuery中CSS()方法示例
2017/04/07 jQuery
AngularJS动态绑定ng-options的ng-model实例代码
2017/06/21 Javascript
Angular 2.0+ 的数据绑定的实现示例
2017/08/09 Javascript
前端必备插件之纯原生JS的瀑布流插件Macy.js
2017/11/22 Javascript
electron + vue项目实现打印小票功能及实现代码
2018/11/25 Javascript
Vue axios全局拦截 get请求、post请求、配置请求的实例代码
2018/11/28 Javascript
Vuejs监听vuex中值的变化的方法示例
2018/12/02 Javascript
JavaScript数据结构与算法之二叉树遍历算法详解【先序、中序、后序】
2019/02/21 Javascript
基于Layui自定义模块的使用方法详解
2019/09/14 Javascript
JS删除数组指定值常用方法详解
2020/06/04 Javascript
微信小程序实现聊天室
2020/08/21 Javascript
vue 中使用print.js导出pdf操作
2020/11/13 Javascript
详解Python中 __get__和__getattr__和__getattribute__的区别
2016/06/16 Python
python 文件查找及内容匹配方法
2018/10/25 Python
python实现定时发送qq消息
2019/01/18 Python
python numpy数组复制使用实例解析
2020/01/10 Python
python3环境搭建过程(利用Anaconda+pycharm)完整版
2020/08/19 Python
服装店营销方案
2014/03/10 职场文书
淘宝店策划方案
2014/06/07 职场文书
2014年小学重阳节活动策划方案
2014/09/16 职场文书
研究生给导师的自荐信
2015/03/06 职场文书
2019年年中职场激励人心语录30条
2019/08/07 职场文书
教你用python实现12306余票查询
2021/06/30 Python
从原生JavaScript到React深入理解
2022/07/23 Javascript