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操作userdata
Apr 27 Javascript
js window.onload 加载多个函数的方法
Nov 02 Javascript
JQuery之拖拽插件实现代码
Apr 14 Javascript
Jquery 的扩展方法总结
Oct 01 Javascript
为什么要在引入的css或者js文件后面加参数的详细讲解
May 03 Javascript
仿百度联盟对联广告实现代码
Aug 30 Javascript
谷歌Chrome浏览器扩展程序开发小记
Jan 06 Javascript
jquery获取select,option所有的value和text的实例
Mar 06 Javascript
Vue.js中extend选项和delimiters选项的比较
Jul 17 Javascript
使用JavaScript实现node.js中的path.join方法
Aug 12 Javascript
vue实现todolist基本功能以及数据存储功能实例详解
Apr 11 Javascript
React+EggJs实现断点续传的示例代码
Jul 07 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作为网站开发语言的原因分享
2012/01/03 PHP
基于PHP静态类的原罪详解
2013/05/06 PHP
php实现文章置顶功能的方法
2016/10/20 PHP
PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法
2017/11/10 PHP
jquery删除提示框弹出是否删除对话框
2014/01/07 Javascript
JS应用正则表达式转换大小写示例
2014/09/18 Javascript
DEDECMS如何为文章添加HOT NEW标志图片
2015/08/14 Javascript
jquery插件tytabs.jquery.min.js实现渐变TAB选项卡效果
2015/08/25 Javascript
今天抽时间给大家整理jquery和ajax的相关知识
2015/11/17 Javascript
基于jQuery实现表格的查看修改删除
2016/08/01 Javascript
Bootstrap Table服务器分页与在线编辑应用总结
2016/08/08 Javascript
JS正则表达式常见用法实例详解
2018/06/19 Javascript
webpack的CSS加载器的使用
2018/09/11 Javascript
浅谈Vue.set实际上是什么
2019/10/17 Javascript
Jquery Datatables的使用详解
2020/01/30 jQuery
[02:41]DOTA2英雄基础教程 冥魂大帝
2014/01/16 DOTA
[02:32]【DOTA2亚洲邀请赛】iceice,梦开始的地方
2017/03/13 DOTA
基于python中theano库的线性回归
2018/08/31 Python
Python安装与基本数据类型教程详解
2019/05/29 Python
python try except返回异常的信息字符串代码实例
2019/08/15 Python
Python内置函数locals和globals对比
2020/04/28 Python
The Hut英国:英国领先的豪华在线百货商店
2019/07/26 全球购物
Claire’s法国:时尚配饰、美容、珠宝、头发
2021/01/16 全球购物
请问软件开发中的设计模式你会使用哪些
2015/05/13 面试题
法律专业应届生自荐信范文
2014/01/06 职场文书
铁路工务反思材料
2014/02/07 职场文书
社区党务公开实施方案
2014/03/18 职场文书
村干部培训方案
2014/05/02 职场文书
本科毕业生自荐信
2014/05/26 职场文书
青年志愿者活动方案
2014/08/17 职场文书
2014年社区党建工作总结
2014/11/11 职场文书
贪污检举信范文
2015/03/02 职场文书
当幸福来敲门英文观后感
2015/06/01 职场文书
python删除csv文件的行列
2021/04/06 Python
Python基础详解之描述符
2021/04/28 Python
Python卷积神经网络图片分类框架详解分析
2021/11/07 Python