jQuery实现搜索页面关键字的功能
发布时间 - 2026-01-10 23:05:09 点击率:次在一篇文章中查找关键字,找到后高亮显示。
具体代码:
<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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# jQuery搜索页面关键字
# jQuery搜索关键字
# jQuery页面关键字
# JQuery+JS实现仿百度搜索结果中关键字变色效果
# jquery+json实现的搜索加分页效果
# 基于jquery的页面划词搜索JS
# jquery中用jsonp实现搜索框功能
# jQuery Jsonp跨域模拟搜索引擎
# jquery 表格排序、实时搜索表格内容(附图)
# jQuery插件select2利用ajax高效查询大数据列表(可搜索、可分页)
# jQuery UI仿淘宝搜索下拉列表功能
# jQuery下拉美化搜索表单效果代码分享
# 基于jQuery实现页面搜索功能
# JavaScript前端页面搜索功能案例【基于jQuery】
# 遍历
# 他们的
# 找了
# 在一
# 到第
# 大家多多
# 清空
# 正则表达式
# padding
# highlight
# color
# background
# solid
# px
# width
# black
# originally
# brain
# attic
# empty
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel怎么创建控制器Controller_Laravel路由绑定与控制器逻辑编写【指南】
如何彻底删除建站之星生成的Banner?
高防服务器:AI智能防御DDoS攻击与数据安全保障
如何在云虚拟主机上快速搭建个人网站?
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
如何使用 jQuery 正确渲染 Instagram 风格的标签列表
JavaScript中如何操作剪贴板_ClipboardAPI怎么用
网站制作免费,什么网站能看正片电影?
购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?
HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】
Laravel storage目录权限问题_Laravel文件写入权限设置
如何快速生成橙子建站落地页链接?
Laravel怎么调用外部API_Laravel Http Client客户端使用
高性价比服务器租赁——企业级配置与24小时运维服务
Laravel如何使用Blade模板引擎?(完整语法和示例)
Python结构化数据采集_字段抽取解析【教程】
Laravel如何配置.env文件管理环境变量_Laravel环境变量使用与安全管理
Laravel如何自定义分页视图?(Pagination示例)
如何快速生成ASP一键建站模板并优化安全性?
EditPlus 正则表达式 实战(3)
Laravel如何实现API资源集合?(Resource Collection教程)
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
如何在阿里云虚拟服务器快速搭建网站?
如何快速搭建高效WAP手机网站?
laravel怎么通过契约(Contracts)编程_laravel契约(Contracts)编程方法
BootStrap整体框架之基础布局组件
浅谈javascript alert和confirm的美化
Laravel如何集成Inertia.js与Vue/React?(安装配置)
javascript基本数据类型及类型检测常用方法小结
高性能网站服务器配置指南:安全稳定与高效建站核心方案
Laravel如何处理异常和错误?(Handler示例)
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
电视网站制作tvbox接口,云海电视怎样自定义添加电视源?
详解MySQL数据库的安装与密码配置
Laravel如何升级到最新的版本_Laravel版本升级流程与兼容性处理
消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
Laravel怎么使用artisan命令缓存配置和视图
为什么php本地部署后css不生效_静态资源加载失败修复技巧【技巧】
免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?
Laravel如何发送邮件和通知_Laravel邮件与通知系统发送步骤
深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?
Laravel如何与Inertia.js和Vue/React构建现代单页应用
Laravel如何使用Scope本地作用域_Laravel模型常用查询逻辑封装技巧【手册】
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
微信公众帐号开发教程之图文消息全攻略
Python自动化办公教程_ExcelWordPDF批量处理案例
如何在服务器上三步完成建站并提升流量?
laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法

