使用jQuery怎么实现一个页面关键字搜索功能

发布时间:2021-01-04 16:46:38

来源:亿速云

阅读:136

作者:Leah

这篇文章给大家介绍使用jQuery怎么实现一个页面关键字搜索功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

具体代码:

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());//将他们的属性去掉;

});

});

}

});

关于使用jQuery怎么实现一个页面关键字搜索功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

html页面如何实现搜索功能,使用jQuery怎么实现一个页面关键字搜索功能相关推荐

  1. 在h5页面中调起支付宝小程序中的某一个页面以及URLScheme 之 支付宝

    在h5页面中调起支付宝小程序中的某一个页面 直接上代码: window.location.href = 'alipays://platformapi/startapp?appId=2021001181 ...

  2. android实现计算器功能吗,利用Android实现一个简单的计算器功能

    利用Android实现一个简单的计算器功能 发布时间:2020-11-20 16:25:01 来源:亿速云 阅读:90 作者:Leah 今天就跟大家聊聊有关利用Android实现一个简单的计算器功能, ...

  3. 基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码

    比如 想跳到 mao.aspx 的页面 的div id="s" 的位置 那么 只用<a href="mao.aspx#s"> 就可实现跳转到指定位置 ...

  4. 一个页面两个或多个swiper vue中一个页面多个swiper互相干扰 swiper轮播冲突

    在vue中如何处理同一个页面中有多个swiper,首先我们需要分2中情况: 1.如果在vue中采用 Vue-Awesome-Swiper,如果采用这种方式的话,可以给每个swiper组件添加一个cla ...

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

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

  6. 一个页面引入另一个页面

    action代码片段 public String execute() {String ret = super.execute();if (RETURN_LIST.equals(ret)) {retur ...

  7. ios把数据传递到另一个页面_iOS 委托 页面之间传递数值

    ios的页面传值方法很多,本博客主要是 通过委托模式 来传值. 将一个页面中时间选择器的值传递到另外一个页面 主要包括三个模块 第一:A类 第二:委托协议 第三:B类 首先定义个一委托UIViewPa ...

  8. JSP页面如何从一个页面传递一个参数到另外一个页面

    (1).一种是重定向跳转,超连<a>就是一种重定向跳转,这样的跳转request对象是传不到下一个页面的,下一个页面得到的request对象是一个新的对象,而不是上一个页面传过来的就得不到 ...

  9. 小程序获取上一个页面或者某个页面内的值

    在小程序中,有时会遇到转跳到下一个页面,但是不能直接把当前页面的集合或者对象整个传入时 ,可以使用JSON.stringify将该对象或集合转换为JSON字符串 let str=JSON.string ...

  10. 小程序-输入关键字搜索,关键字在搜索列表中高亮效果

    这里写自定义目录标题 小程序-输入关键字搜索,关键字在搜索列表中高亮效果 小程序-输入关键字搜索,关键字在搜索列表中高亮效果 第一步:按照网上大神写的思路,在截取首位关键字空格方法 出现找不到repl ...

最新文章

  1. 网络安全中的AI:2021年的六个注意事项
  2. 爱上MVC~为Html.EditorForModel自定义模版
  3. 赛思信安携手太极 助力《甘肃省公安厅大数据警务云》项目
  4. Mac~git学习和应用需要注意的几个点
  5. K8S滚动更新示例演示
  6. Spring Cloud Feign - 内部实现细节
  7. Java-大集合拆分为指定大小的小集合
  8. 一键发布部署vs插件[AntDeploy]开源了
  9. vscode 新建python 终端
  10. Jython中文乱码问题
  11. 怎么做95置信区间图_这种动态的OD图怎么做?简单3步快速搞定
  12. java string 练习_JAVA基础练习之String
  13. Unix系统编程()发送信号kill
  14. WPF窗体的生命周期
  15. Select网络模型
  16. 一道容易栽坑的有趣的面试题(关于js,定时器,闭包等)
  17. linux vi命令怎么使用方法,Linux下 vi 命令用法
  18. android instance区别,Android singleTask 和singleInstance的区别
  19. BIOS中英文对照表
  20. Wuli智能无限的机器人已经上场了,人类律师快靠边站

热门文章

  1. 與情分析系统,包括爬虫、文本摘要、主题分类、情感倾向性识别以及可视化...
  2. matlab计算轮廓曲率半径,【转】求最小曲率半径matlab源程序
  3. ps怎么保存一小块的图片_ps中怎么保存图片
  4. Python基础(七) | 文件、异常以及模块详解
  5. aardio部署_aardio学习笔记-变量与常量
  6. ME525做网络收音机和学外文用了……(安卓4.4.4系统,20190817更新)
  7. phalcon mysql中文乱码_Phalcon查询语言
  8. 生物识别技术在汽车领域带来了巨大变革
  9. 如何优雅的美化kali,实现双桌面环境
  10. 产品新创意,创意产品原型大公开,原来可以这样做!