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

<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>

执行运行代码,效果为:

更多专业前端知识,请上 【猿2048】www.mk2048.com

jQuery实现页面关键词高亮相关推荐

  1. 搜索关键字高亮_Django Haystack 全文检索与关键词高亮

    作者:HelloGitHub-追梦人物[1] 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库[2] 点击本文最下方的"阅读原文"即可获取 博客提供 RS ...

  2. Diango博客--22.Django Haystack 全文检索与关键词高亮

    文章目录 1. Django Haystack 简介 2. 安装 django-haystack和elasticsearch 2 3. 构建容器来运行 elasticsearch 服务 4. 配置 H ...

  3. [转]用 jQuery 实现页面滚动(Scroll)效果的完美方法

    转自: http://zww.me/archives/25144 很多博主都写过/转载过用 jQuery 实现页面滚动(Scroll)效果的方法,但目前搜来的方法大都在 Opera 下有个小 Bug: ...

  4. jQuery Mobile 笔记(1)- jQuery Mobile页面结构

    jQuery Mobile页面解构 jQuery Mobile 的"页面"结构不同于传统的html页面,这样做是为了优化single page application的用户体验. ...

  5. java怎么往右浮动_[Java教程]jQuery实现页面滚动时智能浮动定位

    [Java教程]jQuery实现页面滚动时智能浮动定位 0 2016-08-04 18:00:08 jQuery实现页面滚动时层智能浮动定位_前端开发 * { margin: 0; padding: ...

  6. jQuery Mobile页面返回无需重新get

    jquery mobile新版中没有返回按钮的解决方案 1. 需要在页面源代码的 page 中加上 data-add-back-btn="true" . 2.同时页面的代码中加上一 ...

  7. php jq跳转页面跳转,使用jQuery做页面跳转

    这次给大家带来使用jQuery做页面跳转,使用jQuery做页面跳转的注意事项有哪些,下面就是实战案例,一起来看一下. 所以了,今天我们就来说一说如何在jQuery中跳转到另外一个网页HTML. 其实 ...

  8. wordpress实现搜索页关键词高亮

    http://www.hehaibao.com/wordpress-search-word-highlight/ 今天给网站搜索页加了关键词高亮功能,分享出来,希望对小伙伴们有所帮助. 实现效果如下图 ...

  9. 使用 jQuery Mobile 与 HTML5 开发 Web App (十二) —— jQuery Mobile 页面事件与 deferred

    在系列的上一篇文章<使用 jQuery Mobile 与 HTML5 开发 Web App -- jQuery Mobile 事件详解>中,Kayo 介绍了除页面事件外的其他 jQuery ...

最新文章

  1. thinkpad重装系统不引导_重装系统时,如何判断Windows的启动方式是Legacy还是UEFI?...
  2. kafka消费者如何读同一生产者消息_Kafka消费者生产者实例
  3. go gorm指定别名_GORM入门指南
  4. hash和trie的对比
  5. iframe 自适应高度 跨域
  6. 牛客21783 牛牛的星际旅行
  7. python scikit learn 关闭开源_慕课|Python调用scikit-learn实现机器学习(一)
  8. 王思聪被限制高消费后,官方终于回应:已有解决方案
  9. thumbnails 变黑_phpcms v9图片生成缩略图变成黑色解决方法
  10. python 指定时间内_python获取指定时间段内的随机不重复的时间点
  11. MySQL简明教程-下
  12. c语言串口调试助手源码,串口调试工具 1.02 (软件 + 源码)
  13. Python源码学习(四)
  14. 容器网络之Kubernetes CNI
  15. 用友 uap NC系统单点登录总结
  16. 重庆美食地图(绝对霸道)
  17. guid主分区表损坏如何处理_什么是GPT或GUID分区表
  18. 论文参考文献GB/T 7714格式生成
  19. PHP微信动态二维码活码管理系统源码免授权 独立版 带充值支付
  20. matlab离群值处理,数据平滑和离群值检测

热门文章

  1. Integer包装类特殊之处
  2. 建立时间、保持时间与亚稳态
  3. Java调用跟踪系统_Tracer:在分布式系统中的调用跟踪和日志相关
  4. cf 1059e 思维 贪心 树
  5. 北斗有 35 颗卫星,而 GPS 有 24 颗卫星,为什么二者数量不同?
  6. linux查找设备所在分片,Linux设备驱动统一模型解析
  7. mysql 8服务配置文件my.ini 不生效_2、mysql的启动选项和配置文件介绍
  8. iframe嵌入页面白屏_封闭在家学网页制作!为页面嵌入PDF文件——零基础自学网页制作
  9. webpackjsonp 还原_具有催化CO2还原性能的非贵金属配合物的配体设计
  10. perl 安装GD 出错解决方案