news 2026/8/21 12:11:20

vue3 + ts 输入框对特殊字符进行颜色标识

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
vue3 + ts 输入框对特殊字符进行颜色标识


在后台管理系统中用户输入内容需要对特殊字符进行颜色标识,这里使用到的是elementPlus,因为输入框是字符串无法做颜色标识,只能使用标签形式来做颜色标识。使用定位将渲染元素和输入框重合在一起,输入框背景透明、颜色透明,循环输入元素对每个元素进行判断,是否需要颜色标记,有的话插入一个带class标签span来处理

<template><divclass="keywordInputContainer"><!--和输入层完全重叠,负责渲染标色文字--><divclass="highlightDev"v-html="highlightedText"></div><!--透明,仅负责编辑--><el-inputclass="highlightInput"v-model="inputData"type="textarea"resize="none"@input="handleInput"/></div></template><script lang="ts"setup>constinputData=ref('')consthighlightedText=ref('')consthighlightBracketContent=(inputText:string)=>{if(!inputText)return''letresult=''constinputTextNew=JSON.parse(JSON.stringify(inputText))// 遍历每个字符处理for(leti=0;i<inputTextNew.length;i++){constchar=inputTextNew[i]// 获取字符ASCII编码(十进制)constasciiCode=char.charCodeAt(0)letcharHtml=''// 1. 控制字符(0-31、127)if((asciiCode>=0&&asciiCode<=31)||asciiCode===127){charHtml=`<span class="highlight-text">${char}</span>`}// 2. 显示字符(32-126)elseif(asciiCode>=32&&asciiCode<=126){charHtml=`<span class="highlight-text">${char}</span>`}else{charHtml=char}result+=charHtml;}returnresult;}// 实时输入触发标色consthandleInput=()=>{highlightedText.value=highlightBracketContent(inputData.value);}watch(inputData,handleInput,{immediate:true});</script><style scoped lang="scss">:deep(.highlight-text){background:#fbde28!important;margin:0!important;padding:0!important;border:none!important;// background: none !important;font-weight:inherit!important;font-size:inherit!important;line-height:inherit!important;font-family:inherit!important;vertical-align:baseline!important;}.keywordInputContainer{width:100%;height:100%;position:relative;.highlightDev{position:absolute;top:0;left:0;height:100%;width:100%;padding:8px11px!important;box-sizing:border-box;font-family:"Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei",sans-serif!important;font-size:14px!important;line-height:1.5!important;word-break:break-all;white-space:pre-wrap;overflow-y:auto;pointer-events:none;z-index:1;}// 输入层:透明,仅保留编辑功能:deep(.highlightInput){position:relative;width:100%;height:100%;box-sizing:border-box;z-index:999;.el-textarea__inner{background-color:transparent!important;color:transparent!important;-webkit-text-fill-color:transparent!important;caret-color:#333!important;padding:8px11px!important;font-family:"Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei",sans-serif!important;font-size:14px!important;line-height:1.5!important;word-break:break-all;white-space:pre-wrap;border:1px solid #dcdfe6!important;resize:none!important;&:focus{caret-color:#409eff!important;border-color:#409eff!important;}}}}</style>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/21 11:23:17

Inter字体完整使用指南:从入门到精通

Inter字体完整使用指南&#xff1a;从入门到精通 【免费下载链接】inter The Inter font family 项目地址: https://gitcode.com/gh_mirrors/in/inter Inter字体是专为现代数字屏幕精心设计的开源字体家族&#xff0c;以其出色的可读性和灵活的可变字体特性&#xff0c;…

作者头像 李华
网站建设 2026/8/20 3:09:05

VSCode配置Qiskit代码补全的5个致命错误,你现在可能正犯着!

第一章&#xff1a;VSCode配置Qiskit代码补全的5个致命错误&#xff0c;你现在可能正犯着&#xff01;许多开发者在使用 VSCode 配合 Qiskit 进行量子计算开发时&#xff0c;常常遭遇代码补全失效、模块无法识别等问题。这些问题大多源于配置疏忽&#xff0c;以下是五个常见但致…

作者头像 李华
网站建设 2026/8/20 3:13:31

PEFT自定义扩展终极指南:从入门到精通

PEFT自定义扩展终极指南&#xff1a;从入门到精通 【免费下载链接】peft &#x1f917; PEFT: State-of-the-art Parameter-Efficient Fine-Tuning. 项目地址: https://gitcode.com/gh_mirrors/pe/peft 你是否还在为大型语言模型微调时的显存爆炸而烦恼&#xff1f;是否…

作者头像 李华
网站建设 2026/8/21 18:11:43

提速300%!MiniGPT-4多线程批量推理实战指南

还在为MiniGPT-4单张图片推理速度慢而苦恼吗&#xff1f;&#x1f914; 当你需要处理成百上千张图像时&#xff0c;逐一推理的方式简直让人崩溃&#xff01;本文将为你揭秘如何通过多线程技术彻底释放MiniGPT-4的批量推理潜力&#xff0c;让你的图像理解任务飞起来&#xff01;…

作者头像 李华
网站建设 2026/8/21 14:47:36

图数据库查询慢?立即检查Agent索引配置,8大常见陷阱你中了几个?

第一章&#xff1a;图数据库查询性能瓶颈的根源分析图数据库在处理高度关联数据时展现出强大优势&#xff0c;但在实际应用中&#xff0c;随着数据规模增长和查询复杂度提升&#xff0c;性能瓶颈逐渐显现。深入剖析这些瓶颈的根源&#xff0c;是优化系统响应速度和扩展能力的前…

作者头像 李华