示例代码,关键位置做了注释,请查看代码:

<html>

    <head>
<title>jQuery实现页面关键词高亮</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
} p {
padding: 10px;
margin-bottom: 20px;
} .highlight {
background-color: yellow;
font-weight: bold;
}
</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 src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//点击search按钮时,执行highlight函数
$('#search').click(highlight);
//点击clear按钮时,执行clearSelection函数
$('#clear').click(clearSelection); function highlight() {
//先清空一下上次高亮显示的内容
clearSelection();
//获取输入的关键词
var searchText = $('#text').val();
//创建正则表达式,g表示全局的,如果不用g,则查找到第一个就不会继续向下查找了
var regExp = new RegExp(searchText, 'g');
//遍历段落
$('p').each(function() {
var html = $(this).html();
//将找到的关键词替换,加上highlight属性
var newHtml = html.replace(regExp, '<span class="highlight">' + searchText + '</span>');
//更新段落内容
$(this).html(newHtml);
});
} function clearSelection() {
$('p').each(function() {
//找到所有highlight属性的元素
$(this).find('.highlight').each(function() {
//将highlight样式去掉
$(this).replaceWith($(this).html());
});
});
}
});
</script>
</body> </html>

执行运行代码,效果为:

jQuery实现页面关键词高亮的更多相关文章

  1. 如何通过js使搜索关键词高亮

    给你推荐通过jquery来实现高亮关键词.jquery.textSearch-1.0.js代码: (function($){ $.fn.textSearch =function(str,options ...

  2. jQuery实现页面内锚点平滑跳转

    平时我们做导航滚动到内容都是通过锚点来做,刷的一下就直接跳到内容了,没有一丝的滚动效果,而且 url 链接最后会有“小尾巴”,就像#keleyi,今天我就介绍一款 jquery 做的滚动的特效,既可以 ...

  3. php-设置关键词高亮的字符串处理函数

    /** * 设置关键词高亮的字符串处理函数 * @param [string] $str [要高亮的字符串] * @param array $word_arr [关键词] */function set ...

  4. JQuery 实现页面无刷新

    对于JQuery实现页面无刷新的效果,即:应用这个JQuery这个组件,可以实现在页面上加载数据库中的数据信息,但是并没有给用户页面刷新的感觉,这样既可以有效的进行数据交互,也可以不妨碍用户的其他操作 ...

  5. js 和 jquery 获取页面和滚动条的高度 视口高度文档高度

    js 和 jquery 获取页面和滚动条的高度 //页面位置及窗口大小 function GetPageSize() { var scrW, scrH; if(window.innerHeight & ...

  6. 关于jquery计算页面元素数量

    这段jquery计算页面元素数量代码,能不能刷新页面直接输出数量,而不用点计算按钮 <scriptsrc="http://ajax.googleapis.com/ajax/libs/j ...

  7. 用JS或jQuery访问页面内的iframe,兼容IE/FF

    用JS或jQuery访问页面内的iframe,兼容IE/FF js或者jQuery访问页面中的框架也就是iframe.注意:框架内的页面是不能跨域的! 假设有两个页面,在相同域下. index.htm ...

  8. 通过javascript库JQuery实现页面跳转功能代码

    通过javascript库JQuery实现页面跳转功能代码的四段代码实例如下. 实例1: 1 2 3 4 $(function(){ var pn = $("#gotopagenum&quo ...

  9. js原生 + jQuery实现页面滚动字幕

    js原生/jQuery实现页面滚动字幕效果 17:45:49 在新闻列表或者文章列表信息等页面中很容易要求实现字幕滚动的效果,以下为简单的实现页面中滚动字幕的效果 1.jQuery实现页面滚动字幕效果 ...

随机推荐

  1. VHDL语言实现的任意整数分频器

    fpga中,一般外接的晶振是50Mhz,如果电路中一个模块需要25mhz时钟,那么进行一个2分频,这个是相当容易的,下面是一种方法,还有可以用一个二进制计数器实现.这里就不写代码了.easy.同样的原 ...

  2. solaris之cpu

    一. Solaris的处理器硬件系统架构 Solaris支持多种处理器系统架构:SPARC.x86和x64.x64即AMD64及EMT64处理器.在版本2.5.1的时候,Solaris曾经一度被移植到 ...

  3. mt7620 wireless驱动特性意外发现

    前言 今天又客户反映无线參数SSID编程了HT_AP0, 同事通过后台给他改动后反映给我,我想不正确啊,难道是无线驱动crash了?那应该不能玩才对啊... 追查线索 我们的路由器会定期汇报数据SSI ...

  4. 使用 NuGet 更新套件時將 jQuery 升級到 2.0.2 應該如何降級

    我們在 Visual Studio 2012 裡面會使用 NuGet 管理員管理那些常用的開發函式庫,例如jQuery.Json.NET. EntityFramework.ELMAH.… 等等.各位可 ...

  5. Java Web应用软件保护方法

    1 代码混淆概述目前针对Java源文件方法主要有以下几种方法:本地编译技术.数字水印技术.ClassLoader加密技术以及代码混淆技术.Java本地编译是指将Java应用程序编译成本地应用程序,如W ...

  6. tomcat sso 配置

    源: http://www.oecp.cn/hi/single/blog/349 为了实现单点登录,做了个CAS SSO单点登录实例,经过反复的琢磨和修改终于成功了,现将CAS SSO单点登录实例详细 ...

  7. 前端新手如何安装webstorm ,初步搭建react项目

    下载安装webstorm:配置成功: 配置成功后就可以开启webstorm项目了.(存微信收藏..) 1:在webstorm下配置node环境: 2:完成之后: React官方脚手架地址: https ...

  8. [Android] Anreoid repo 切换分支

    reference : http://blog.csdn.net/lihui130135/article/details/40858885 如果已经有android仓库但是还不是最新的,想切换到And ...

  9. Python3.6学习笔记(五)

    网络编程 网络程序出现的比互联网要早很多,实现方式主要依靠网络上不同主机间进程的通信,通信协议最重要的是TCP/IP协议.在这两个协议基础上还有很多更高级的协议,包括HTTP.SMTP等.要进行两个主 ...

  10. [12] 扇形体(Fan)图形的生成算法

    顶点数据的生成 bool YfBuildFunVertices ( Yreal radius, Yreal degree, Yreal height, Yuint slices, YeOriginPo ...