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

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>

http://www.xdnf.cn/news/13606.html

相关文章:

  • uniapp 腾讯云 COS 文件管理进阶(文件夹分类与批量操作)
  • ThreadLocal为什么会导致内存泄漏(详细讲解)
  • Android NumberPicker使用大全
  • 数据管理四部曲:元数据管理、数据整合、数据治理、数据质量管控
  • ArcGIS数据管理与转换、地图制作、数据制备、矢量空间分析、栅格空间分析、空间插值、三维分析、高级建模
  • Packagerun:VSCode 扩展 快捷执行命令
  • Python第八周作业
  • ZeroTier+CCproxy+Proxifier实现内网穿透和流量转发
  • 让报表成为生产现场的“神经系统”,推动管理自动化升级
  • 第30节 Node.js C/C++ 插件
  • Appium+python自动化(二十一)- Monkey指令操作手机
  • Vue3+TypeScript实现访问者模式
  • PyTorch深度学习框架60天进阶学习计划-第57天:因果推理模型(二)- 高级算法与深度学习融合
  • ARM 和 x86_64是什么关系
  • 论文阅读:speculative decoding
  • 校赛2025迎新杯题解
  • 欧盟RED网络安全标准EN 18031-2的要求
  • 什么是序列化?反序列化? 场景使用? 怎么实现???
  • 「ECG信号处理——(17)基于小波熵阈值的R峰检测(与时域-频域-多尺度小波法对比)」2025年6月12日
  • Docker 安装 Oracle 12C
  • 大厂Java技术面试实录:从基础到架构,谢飞机的面试之旅
  • springboot+mybatis面试题
  • MySQL行锁、记录锁、间隙锁、临建锁、意向锁、表锁
  • 体育赛事直播平台的数据架构:从实时统计到深度洞察
  • 运放负反馈电路原理分析
  • 卡通幼儿园教育通用可爱PPT模版分享
  • 瑞芯微 MIPI D-PHY 接收器(RX)驱动学习笔记
  • 达梦数据库(DM)用户名大小写处理规则
  • MAC-苹果电脑专业卸载工具AppCleaner
  • C++ Vector深度解析:动态组的底层机制与实战指南