尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
记录一个需求,多行输入框 #变色输入话题,特殊符号,空格结束变色,且支持话题文字修改和删除,复制粘贴,限长500字,超出提示
js template div classbox p v-ifp classp请输入标题.../p div langzh-CN classdiv :contenteditablebox refeditor bluronBlur compositionstartonInput1(false) compositionendonInput1(true) inputonInput2(true) clickclick keyup.upgetData keyup.leftgetData keyup.rightgetData keyup.downgetData /div span classpos{{ string1.length }}/{{ textLength }}/span div classadd clickcar(#)# 添加话题/div /div /template script export default { name: RichText, data() { return { p: true, box: true, divspan: , string1: , // 内容 index: 0, // 光标位置 textLength: 500, // 文本的最大长度 list: [], // 内容中的关键词列表 kg: true, // 当前是否使用输入法输入 ischange: false, }; }, props: { value: { type:String, default: }, }, watch: { string1() { if (this.string1.length ! 0) { this.p false; } else { this.p true; } }, value: { handler: function (newValue, oldValue) { //判断赋值展示 this.$nextTick(() { const div this.$refs.editor; div.innerHTML newValue.videoName; this.onInput(true); }); }, deep: true, }, }, mounted() { this.getFocusPosition(); if (this.nameVideo?.videoName) { this.car(this.nameVideo.videoName); } }, methods: { // 使用上下左右按键在输入框中移动时需要获取光标的位置 getData(data) { this.index this.getFocusPosition(); }, // 每次点击div就重新获取一次当前光标的位置 click(a) { this.index this.getFocusPosition(); }, // 失去焦点触发传值,value是传进来的值 onBlur() { this.ischange true; this.uploadVideo.videoName this.string1; }, model() {}, // 使用delete按键删除 部分删减还没有全部实现还是出现跨标题的bug后续有空慢慢修复这个事件没啥用删除掉前面的bug就没了 handleDelete(event) { event.preventDefault(); const div this.$refs.editor; const text this.$refs.editor.innerText; const selection window.getSelection(); const selectedText selection.toString(); if (selectedText selectedText text) { div.innerHTML ; this.string1 ; this.index 0; this.onInput(true); return; } if (selectedText selectedText text) { div.innerHTML ; this.string1 ; this.index 0; this.onInput(true); return; } if (selectedText selectedText.indexOf(\n) -1) { let aa ; if ( this.index - selectedText.length this.index text.slice(this.index - selectedText.length, this.index) selectedText ) { aa text.slice(0, this.index - selectedText.length) text.slice(this.index); this.index this.index - selectedText.length; div.innerHTML aa; this.string1 this.$refs.editor.innerText; return this.onInput(true); } if ( this.index selectedText.length this.index text.slice(this.index, this.index selectedText.length) selectedText ) { aa text.slice(0, this.index) text.slice(this.index selectedText.length, text.length); div.innerHTML aa; this.string1 this.$refs.editor.innerText; return this.onInput(true); } div.innerHTML aa; this.string1 this.$refs.editor.innerText; this.onInput(true); return; } if (this.index 0) { const aa text.slice(0, this.index - 1) text.slice(this.index); div.innerHTML aa; this.string1 this.$refs.editor.innerText; this.index--; this.onInput(true); } }, //插入文本 car(zfc) { let le this.textLength - this.string1.length; if ((this.textLength this.string1.length this.string1.length 0) || le zfc.length) { this.$message({ type: error, message: 文字长度最大不超过500, }); return; } const index this.index; let arr this.string1.split(); arr.splice(index, 0, ...zfc.split()); const div this.$refs.editor; div.innerHTML arr.join(); this.index zfc.length; this.onInput(true, true); }, // 每次输入都会触发 onInput2(i) { const div this.$refs.editor; this.index this.getFocusPosition(); this.string1 this.$refs.editor.innerText; if (this.textLength this.string1.length this.string1.length 0 this.kg) { this.$message({ type: error, message: 文字长度最大不超过500, }); div.innerHTML this.string1.slice(0, this.textLength); div.blur(); } if (i this.kg) { this.onInput(i); } this.ischange true; this.uploadVideo.videoName this.string1; }, // 判断当前输入是否使用输入法 onInput1(e) { if (e) { this.kg true; if (this.textLength this.string1.length this.string1.length 0 this.kg) { const div this.$refs.editor; this.$message({ type: error, message: 文字长度最大不超过500, }); div.innerHTML this.string1.slice(0, this.textLength); div.blur(); } } else { this.kg false; } if (this.kg) { this.onInput(this.kg); } }, // 匹配关键词使用text替换e是否执行替换j暂时无用 onInput(e, j) { let text this.$refs.editor.innerText; this.string1 text; const re /#[^\s.,;?!。’、](?[\s.,;?!。’、])|#[^\s]/g; const newArr text.match(re); let matches newArr newArr.map((item) item.replace(/^#/, #)).sort((a, b) b.length - a.length); matches matches.forEach((item, i) { if (item #) { matches.splice(i, 1); } matches[i] item.slice(item.lastIndexOf(#), item.length); }); this.list matches; let div this.$refs.editor; if (matches e) { for (let i 0; i matches.length; i) { const match matches[i]; const regex new RegExp(${match}(?[\\s.,;?!。’、]|$)); text text.replace(regex, text classspan refspan$/text); div.innerHTML text; } } // 定位焦点光标 const range document.createRange(); const sel window.getSelection(); let index this.index; for (let i 0; i div.childNodes.length; i) { if (index div.childNodes[i].textContent.length) { index index - div.childNodes[i].textContent.length; } else { if (div.childNodes[i].className span) { range.setStart(div.childNodes[i].childNodes[0], index); } else { range.setStart(div.childNodes[i], index); } range.collapse(true); sel.removeAllRanges(); sel.addRange(range); return; } } }, // 获取光标位置 getFocusPosition() { const selection window.getSelection(); const range document.createRange(); selection.anchorNode range.setStart(selection.anchorNode, 0); const textNodes this.getTextNodes(this.$refs.editor); let index 0; // 遍历每一个文本节点 for (let i 0; i textNodes.length; i) { const textNode textNodes[i]; const length textNode.textContent.length; if (textNode selection.anchorNode) { index selection.anchorOffset; break; } index length; } return index; }, // 获取到每一个子节点 getTextNodes(node) { const textNodes []; if (node.nodeType Node.TEXT_NODE) { textNodes.push(node); } else { const children node.childNodes; for (let i 0; i children.length; i) { const child children[i]; textNodes.push(...this.getTextNodes(child)); } } return textNodes; }, } }; /script style .div { width: 580px; height: 115px; padding: 5px 15px; line-height: 16px; /* border: 1px #ccc transparent; */ overflow: auto; z-index: 999; border-radius: 4px; -webkit-user-modify: read-write-plaintext-only; } .div::-webkit-scrollbar { display: none; /* Chrome Safari */ } .p { position: absolute; top: 0px; left: 20px; color: #d3d1d1; z-index: -999999; } .span { color: #4f72f6; } .box { width: 600px; height: 106px; position: relative; padding-bottom: 40px; border: 1px #ccc solid; border-radius: 4px; } .pos { position: absolute; color: #ccc; bottom: 3px; right: 20px; } .colorred { color: #fe5353; } .add { width: 92px; margin-top: 25px; font-weight: 400; font-size: 14px; line-height: 24px; color: #4f72f6; background: #f0f3ff; border-radius: 4px; text-align: center; } .add:hover { background-color: #4f72f6; color: #f0f3ff; cursor: pointer; } /style
RELATED

相关推荐

DS4Windows完整指南:5分钟让PS手柄在Windows上畅玩所有游戏

DS4Windows完整指南:5分钟让PS手柄在Windows上畅玩所有游戏

DS4Windows完整指南:5分钟让PS手柄在Windows上畅玩所有游戏 【免费下载链接】DS4Windows Like those other ds4tools, but sexier 项目地址: https://gitcode.com/gh_mirrors/ds/DS4Windows 还在为Windows游戏不兼容PlayStation手柄而烦恼吗?每次…

📅 2026/11/14 9:04:48
相机位姿估计:PnP问题原理与工程实践

相机位姿估计:PnP问题原理与工程实践

1. 相机位姿估计的核心原理与应用场景在计算机视觉和机器人定位领域,相机位姿估计是一个基础但至关重要的技术。简单来说,就是通过图像信息计算出相机在三维空间中的位置和朝向。这个技术在我们日常生活中其实无处不在——从手机AR贴纸到自动驾驶车辆的定…

📅 2026/11/21 1:07:53
终极小说下载工具:一键保存200+网站小说为TXT/EPUB格式

终极小说下载工具:一键保存200+网站小说为TXT/EPUB格式

终极小说下载工具:一键保存200网站小说为TXT/EPUB格式 【免费下载链接】novel-downloader 一个可扩展的通用型小说下载器。 项目地址: https://gitcode.com/gh_mirrors/no/novel-downloader 你是否曾因心爱的小说突然从网站上消失而懊恼?在数字时…

📅 2026/11/21 22:01:43
MORE NEWS

更多资讯

📰

工程电工证复审流程怎么考?工地太忙根本没时间复习考试,多少钱才够?

工程电工证复审流程怎么考?工地太忙根本没时间复习考试,多少钱才够? 工地太忙根本没时间复习考试,工程电工证复审流程怎么考?复审多少钱?这些问题天天折磨着我们这些在工地干活的电工。别急,下面我来详细说说,帮你少走弯路,少花冤枉钱。…

📰

工地太忙根本没时间复习考试?内部消息教你搞定高压低压电工证图片办理

工地太忙根本没时间复习考试?内部消息教你搞定高压低压电工证图片办理 工地太忙根本没时间复习考试?你不是一个人在战斗,我见过太多电工兄弟因为赶工期、跑现场、盯安全,连考试复习的时间都挤不出来,结果证书一拖再拖,机会一失再失。特别是高压低压电工…

📰

电工证过期了怎么报名?纸质电工证申请流程全解析

电工证过期了怎么报名?纸质电工证申请流程全解析 证书过期了不知道怎么复审延期,这种焦虑你肯定经历过。尤其是像我们濮阳的中小施工企业负责人,手里握着一堆电工证,要是到期不处理,不仅影响项目推进,还可能面临停工整顿的处罚。今天咱们就聊聊…

📰

阜阳市特种工证考试报名学历门槛低吗?电子证书怎么用?

阜阳市特种工证考试报名学历门槛低吗?电子证书怎么用? 学历不高怕报不上名,这是很多人报考阜阳市特种工证考试时的第一反应。但实际情况可能和你想的不一样。我跟你说,只要你能看懂报名表,能填清楚身份证号和联系方式,就完全没问题。阜阳市特种工证考试…

📰

合川高级电工证实操考试心里没底怕挂科?良心建议帮你稳过

合川高级电工证实操考试心里没底怕挂科?良心建议帮你稳过 实操考试心里没底怕挂科,这是很多准备考合川高级电工证的学员最真实的感受。特别是初次报考的人员,面对复杂的仪器设备、严格的考核标准,一不小心就容易掉进坑里。别慌,今天就来给你一些建议,帮…

📰

电工证分为多少 值不值得考?选对培训机构是关键

电工证分为多少 值不值得考?选对培训机构是关键 不知道去哪报名怕被中介坑,这种焦虑很多人都有,尤其是第一次接触电工证的朋友们。你是不是也担心自己花了一大笔钱,结果考了个假证?或者找的培训机构不靠谱,耽误时间和精力?别急,今天我就带你全面了解…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬