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

js实现输入高亮@和#后面的内容

效果如下:支持连续输入

<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title></title><style>* {box-sizing: border-box;margin: 0;padding: 0;}body {min-height: 100vh;display: flex;justify-content: center;align-items: center;padding: 20px;}.container {width: 100%;max-width: 800px;background: white;border-radius: 16px;box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);overflow: hidden;}.content {padding: 30px;}.editor-container {position: relative;margin-bottom: 30px;}#editor {min-height: 200px;border: 2px solid #e0e0e0;border-radius: 8px;padding: 15px;font-size: 16px;line-height: 1.6;outline: none;transition: border-color 0.3s;background: white;overflow-y: auto;}#editor:focus {border-color: #3498db;box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);}.highlight {background-color: #fff9c4;border-radius: 3px;padding: 0 2px;color: #e74c3c;font-weight: 500;}</style>
</head><body><div class="container"><div class="content"><div class="editor-container"><div id="editor" contenteditable="true" placeholder="输入内容..."></div></div></div></div><script>const editor = document.getElementById('editor');function saveCursorPosition() {const selection = window.getSelection();if (selection.rangeCount === 0) return null;const range = selection.getRangeAt(0);const preCaretRange = range.cloneRange();preCaretRange.selectNodeContents(editor);preCaretRange.setEnd(range.endContainer, range.endOffset);return {startOffset: preCaretRange.toString().length,range: range};}function restoreCursorPosition(startOffset) {const selection = window.getSelection();selection.removeAllRanges();let charCount = 0;let nodeStack = [editor];let node, foundStart = false;let range = document.createRange();range.setStart(editor, 0);range.collapse(true);while (!foundStart && (node = nodeStack.pop())) {if (node.nodeType === Node.TEXT_NODE) {const nextCharCount = charCount + node.length;if (!foundStart && startOffset >= charCount && startOffset <= nextCharCount) {range.setStart(node, startOffset - charCount);foundStart = true;}charCount = nextCharCount} else {const children = node.childNodes;for (let i = children.length - 1; i >= 0; i--) {nodeStack.push(children[i])}}}if (foundStart) {range.collapse(true);selection.addRange(range);}}function applyHighlighting() {const cursorPosition = saveCursorPosition();const text = editor.textContent;const tags = [];editor.innerHTML = text.replace(/([@#][^\s]+)/g, (match) => {tags.push(match);return `<span class="highlight">${match}</span>`;});// 恢复光标位置if (cursorPosition) {restoreCursorPosition(cursorPosition.startOffset);}}let isComposing = false;editor.addEventListener('compositionstart', () => {isComposing = true;});editor.addEventListener('compositionend', () => {isComposing = false;applyHighlighting();});editor.addEventListener('input', () => {if (!isComposing) {applyHighlighting();}});applyHighlighting();</script>
</body></html>

相关文章:

  • 第六章网络互联设备
  • 「Java EE开发指南」如何用MyEclipse创建一个WEB项目?(一)
  • 在服务器上使用 Docker 部署 Node.js 后端服务和前端项目
  • VScode使用npm启动项目以及npm install ,npm start报错问题处理
  • java使用aspose读取word里的图片
  • Word 文件转md文件 在 Word 中没有直接将文档另存为 Markdown(.md)格式的选项,但你可以使用一些工具或手动转换来实现
  • npm ERR! @biomejs/biome@1.9.4 postinstall: `node scripts/postinstall.js`
  • 解决npm install 一直卡着不动,npm install --verbose
  • Spring Cloud Gateway 动态路由实现方案
  • 怎样解决在ubuntu 22.04上QT: DataVisualization控件显示黑屏的问题
  • Doris集群安装部署
  • Rust 学习笔记:关于通过异步实现并发的练习题
  • 《C++初阶之类和对象》【类 + 类域 + 访问限定符 + 对象的大小 + this指针】
  • DEVICENET转MODBUS TCP网关连接DeviceNet数字远程IO模块配置案例
  • uniapp小程序不支持动态组件问题
  • 更进一步深入的研究ObRegisterCallBack
  • 【iSAQB软件架构】以架构为中心的开发方法
  • node-red的http-request组件调研三方接口请求参数为form-data解决方案
  • 基于算力魔方与PP-OCRv5的OpenVINO智能文档识别方案
  • 零基础RT-thread第一节:串口通信UART
  • 电子商务网站调研报告/微信搜一搜怎么做推广
  • 公司网站可以自己建立吗/关键词搜索排名怎么查看
  • wordpress 隐藏相关文章/seo推广收费标准
  • 查房价的官方网站/百度免费安装下载
  • 江阴哪里有做网站推广/百度怎么推广广告
  • 企业网站建设流程与方法 论文/合肥百度推广优化