当前位置: 首页 > news >正文

vue 识别一个高亮组件全局

hightLight.js

import { ref, watch, nextTick } from 'vue';// 共享关键词状态
const currentKeyword = ref('');export const setGlobalHighlightKeyword = (keyword) => {currentKeyword.value = (keyword || '').trim();
};// 清除高亮
export const clearGlobalHighlight = (container = document.body) => {const highlighted = container.querySelectorAll('span[style*="background: #555"]');highlighted.forEach(span => {if (span.parentNode) {const textNode = document.createTextNode(span.textContent);span.replaceWith(textNode);}});
};const highlightNode = (node, keyword) => {if (!keyword || !node?.textContent) return;const parent = node.parentNode;if (!parent || !parent.isConnected) return;const text = node.textContent;const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');const regex = new RegExp(`(${escapedKeyword})`, 'gi');if (!regex.test(text)) return;const temp = document.createElement('div');text.split(regex).forEach(part => {if (part.toLowerCase() === keyword.toLowerCase()) {const span = document.createElement('span');span.style.cssText = 'color:#ffff00;background: #555;font-weight: bold; font-size: 13px;';span.textContent = part;temp.appendChild(span);} else {temp.appendChild(document.createTextNode(part));}});if (!node.parentNode) return;const nextSibling = node.nextSibling;node.replaceWith(temp);if (nextSibling) {while (temp.lastChild) {parent.insertBefore(temp.lastChild, nextSibling);}} else {while (temp.firstChild) {parent.appendChild(temp.firstChild);}}
};const walkAndHighlight = (element, keyword) => {if (!element.isConnected && element !== document.body) return;const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null);const nodes = [];let node;while (node = walker.nextNode()) {const parent = node.parentNode;if (parent &&['INPUT', 'TEXTAREA', 'SCRIPT', 'STYLE'].includes(parent.tagName) ||parent.isContentEditable) continue;nodes.push(node);}// 逆序处理,避免 DOM 变化影响遍历nodes.reverse().forEach(n => highlightNode(n, keyword));
};export const GlobalHighlight = {mounted(el) {if (currentKeyword.value) {clearGlobalHighlight(el);walkAndHighlight(el, currentKeyword.value);}// 监听关键词变化watch(currentKeyword, (newVal) => {nextTick(() => {if (el.isConnected) {clearGlobalHighlight(el);if (newVal) {walkAndHighlight(el, newVal);}}});});},updated(el) {nextTick(() => {if (el.isConnected) {clearGlobalHighlight(el);if (currentKeyword.value) {walkAndHighlight(el, currentKeyword.value);}}});},unmounted(el) {clearGlobalHighlight(el);}
};export default GlobalHighlight;

应用:main.js 中

import GlobalHighlight, { setGlobalHighlightKeyword } from "~/composables/globalHighlight"

vue文件中:

import { setGlobalHighlightKeyword,clearGlobalHighlight} from '~/composables/globalHighlight';// 点击搜索按钮
const handleSearch = async () => {await nextTick();if (queryParams.ORDER_NO) { setGlobalHighlightKeyword(queryParams.ORDER_NO) }if (queryParams.IVC_NO) { setGlobalHighlightKeyword(queryParams.IVC_NO) }
};// 重置查询条件
const handleReset = () => {clearGlobalHighlight();
}

效果: 

http://www.dtcms.com/a/465931.html

相关文章:

  • RFID与机械臂协同:构建智能产品溯源新范式
  • 生活馆网站开发背景网站制作如皋
  • 第二章数据预处理:公式Python代码实现
  • CSS的重绘和重排是什么?如何减少css的重绘和重排?
  • 哪个网站可以做专业兼职程序员用的编程软件
  • 国内云服务器免费优化培训学校
  • Linux多进程:查看当前哪个进程正在使用共享数据区的互斥锁文件
  • 【MySQL SQL语句实战】设计表,建表语句,数据插入,实战案例包括:简单查询、汇总统计、分组分析、多表关联、窗口函数
  • 系统设计-高频面试题(更新中...)
  • IntelliJ IDEA使用经验(十五):SQL脚本文件转为数据库控制台
  • 【实时Linux实战系列】内核跟踪点(Tracepoints)与用户态探针(UST)的协同调试
  • Linux 进程通信——消息队列与信号量
  • 备案ip 查询网站查询网站小说一键生成动漫
  • 做养生产品哪个网站好嘉兴网站建设网址
  • Vue3中实现全局双向绑定变量
  • C语言数据结构-排序
  • 【三维重建-对极几何】极线约束(Epipolar Constraint)
  • LeetCode算法日记 - Day 68: 猜数字大小II、矩阵中的最长递增路径
  • WSL 安装与卸载
  • app和微网站的对比河源网站建设
  • 新乡网站建设哪家好备案 网站名称
  • 版本控制器 git(5)--- git 标签管理
  • BShare HTTPS 集成与排查实战,从 SDK 接入到 iOS 真机调试(bshare https、签名、回调、抓包)
  • 基于同步压缩连续小波变换(SS-CWT)的微震图像去噪与起始检测
  • 太原网站建设工作室wordpress的内链插件
  • 简述网站开发流程 旅游做网站送邮箱
  • 湖北省住房建设厅网站网站备案登记查询系统
  • uri: mongodb://jack:123456@localhost://27017 数据库访问其他的写法
  • 在K8s中,seaweedFS 和 Longhorn 的比较
  • 146、【OS】【Nuttx】【周边】效果呈现方案解析:特殊变量$