在一篇文章中查找关键字,找到后高亮显示。

具体代码:

Search

p { border:1px solid black;width:500px;padding:5px;}

.highlight { background-color:yellow; }

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.

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.

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.

$(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, ''+searchText+'');//将找到的关键字替换,加上highlight属性;

$(this).html(newHtml);//更新文章;

});

}

function clearSelection()

{

$('p').each(function()//遍历

{

$(this).find('.highlight').each(function()//找到所有highlight属性的元素;

{

$(this).replaceWith($(this).html());//将他们的属性去掉;

});

});

}

});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

ajax 实现关键字搜索,jQuery实现搜索页面关键字的功能相关推荐

  1. 用jQuery实现返回页面顶部的功能

    页面中有返回顶部的图标用起来使我们预览页面更方便,这个功能也是在网站中常见的功能,今天研究了一个简单的例子,用jQuery实现返回顶部的功能: css代码如下: *{ margin:0; paddin ...

  2. 静态html页面关键字搜索高亮

    静态html页面关键字搜索高亮 最近项目要实现一个页面关键字搜索,刚好上网找了一款不错的插件,叫mark.js,官网地址,下面演示一个基于jq的案列,这个插件可以不依赖任何插件也可以实现这个功能,详情 ...

  3. 仿 淘宝搜索栏,实现 用户输入搜索关键字时可以显示 搜索提示 的效果 Ajax

    要实现的 UI 功能 依赖文件: jQuery https://jquery.com/ art-template 模板引擎 https://aui.github.io/art-template/ 实现 ...

  4. 【Python爬虫教学】百度篇·手把手教你抓取百度搜索关键词后的页面源代码

    [开门见山] 最近整理了下之前做过的项目,学的东西不少,乱七八糟.打算写点关于 Python 爬虫的东西,新人一枚,还望大佬们多多担待,别把我头给打歪了. 前面我先磨叽磨叽些基础的东西,对爬虫新人友好 ...

  5. 移动搜索关键字SEO:如何添加移动关键字!

    添加关键字 2017年SEO的主要变化是搜索重点从PC转移到了移动. 众所周知,在Bear Palm发布之后,移动变得越来越重要,而且移动索引的优先级高于PC. 看,移动端的流量也远远超过了PC端. ...

  6. 亚马逊关键词搜索API接口(item_search-按关键字搜索亚马逊商品接口),亚马逊API接口

    亚马逊关键词搜索API接口(item_search-按关键字搜索亚马逊商品接口),亚马逊API接口可以获取到整个页面上看的到的数据,包括:商品ID,商品标题,商品图片,优惠价,价格,价格范围,销量,卖 ...

  7. 动态搜索广告智能查找匹配关键字

    如果您希望让与您业务相关的搜索触发您的广告(包括您未曾想到的搜索),动态搜索广告可以助您一臂之力! 创建关键字列表的挑战 要针对与您业务相关的用户搜索创建关键字列表,不仅挑战重重,而且也耗费时间.随着 ...

  8. 谷歌黑客关键字搜索_Google将搜索量添加到关键字工具

    谷歌黑客关键字搜索 Google announced yesterday evening that it was adding search volume numbers to its AdWords ...

  9. jq中法可以发起ajax请求的方法有,jQuery实现监控页面所有ajax请求的方法

    本文实例讲述了jQuery实现监控页面所有ajax请求的方法.分享给大家供大家参考,具体如下: 你是不是有遇到这样的问题:页面发起两个ajax请求,希望它们都成功以后,再做一个动作? 很容易想到的解决 ...

最新文章

  1. 利用java反射机制,子类从父类拷贝属性--解决信息修改类设计的模式
  2. 神经网络模型中class的forward函数何时调用_用Keras从零开始6步骤训练神经网络
  3. python怎么添加csv模式_python对csv文件追加写入列的方法
  4. Win8离线安装.net framework组件
  5. 在多节点集群中运行Cassandra
  6. 最强战队实力交锋,算法“视”界杯终极一战即将上演
  7. [导入]MsAjax Lib- Date.format 函数
  8. kubernetes视频教程笔记 (6)-kubernetes集群安装
  9. 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第4节 等待唤醒机制_4_Object类中wait带参方法和notifyAll方法...
  10. 【技巧记录】如何批量制作文件夹/文件夹名
  11. ESL3.6 几种线性回归方法比较学习笔记(含协方差相关系数概念)
  12. 曙光服务器硬盘架,曙光服务器硬盘阵列
  13. UI库——muse-ui
  14. Linux登录mysql时 密码正确登录不进去,没有密码却能登进去
  15. java继承求三角形圆形面积_java求三角形面积
  16. 简单内存泄漏检测方法,解决Detected memory leaks!问题 .
  17. 施乐服务器怎么显示浏览器,怎么才能显示出电脑IE浏览器的菜单栏和工具栏
  18. 中国银河证券公募基金分类体系的理论依据与规则基础
  19. JSP中四种作用域的不同
  20. Google AppSheet: 无需编程构建零代码应用

热门文章

  1. 键盘连接在计算机的,技巧:如何在计算机键盘上输入连字符和破折号?
  2. 崎岖回归之路 圆明园十二生肖兽首铜像的故事
  3. python爬取淘宝商品做数据挖掘
  4. 各大厂商企业级BOM解决方案PK
  5. ultraedit编译java_在UltraEdit中编译和运行Java程序
  6. 谷歌浏览器获取计算机用户名,谷歌Chrome浏览器如何删除记住的账号和密码
  7. C语言实验报告册-20163a,C语言实验报告册20163a-资源下载人人文库网
  8. 黑客攻击入侵流程及常见攻击工具
  9. 2019年最新最全的医疗网站优化方案
  10. Jmeter wrk ab压测软件对比