vue中根据html动态渲染内容

需求:根据数据中的html,因为我是在做填空,所以是需要将html中的_____替换成input,由于具体需求我使用的是元素contenteditable代替的可编辑的input

html部分

<div class="wrap"><component :is="renderedContent" ref="wrap_component" /></div>

js部分

// 这个是为了保证输入的时候光标保持在最后
const moveCursorToEnd = (element: HTMLElement) => {const range = document.createRange();const selection = window.getSelection();// 找到最后一个文本节点let lastTextNode: Text | any = null;const traverseNodes = (node: Node) => {if (node.nodeType === Node.TEXT_NODE) {lastTextNode = node as Text;}for (let i = 0; i < node.childNodes.length; i++) {traverseNodes(node.childNodes[i]);}};traverseNodes(element);if (lastTextNode) {range.setStart(lastTextNode, lastTextNode.textContent?.length || 0);range.collapse(true);if (selection) {selection.removeAllRanges();selection.addRange(range);}} else {range.setStart(element, 0);range.collapse(true);if (selection) {selection.removeAllRanges();selection.addRange(range);}}// 兼容性处理:确保元素获取焦点element.focus();if (document.activeElement !== element) {element.focus();}
};// 计算属性,用于生成渲染内容
const renderedContent = computed(() => {if (!itemConf.value.customConf?.inputHtml) return null;const parts = itemConf.value.customConf.inputHtml.split(/_{1,}/);let nodes: any = [];parts.forEach((part, index) => {if (part) {const replacedSpaces = part.replace(/ /g, '&nbsp;');const replacedPart = replacedSpaces.replace(/<div>/g, '<br>').replace(/<\/div>/g, '');nodes.push(h('span', { class: 'custom-span', innerHTML: replacedPart }));}if (index < parts.length - 1) {if (!inputValues.value[index]) {inputValues.value[index] = '';}if (!isInputFocused.value[index]) {isInputFocused.value[index] = false;}if (!isClearIconClicked.value[index]) {isClearIconClicked.value[index] = false;}if (!clearIconHideTimer.value[index]) {clearIconHideTimer.value[index] = 0;}const clearIcon = h(ElIcon,{class: ['clear_icon',{'is-hidden':inputValues.value[index].length === 0 ||itemConf.value.baseConf.isReadOnly ||!isInputFocused.value[index],},],onClick: () => {if (!itemConf.value.baseConf.isReadOnly) {isClearIconClicked.value[index] = true;inputValues.value[index] = '';if (inputRefs.value[index]) {inputRefs.value[index].innerText = '';}adjustInputWidth(index);handleChange(itemConf.value.customConf.inputGroup[index], '');// 点击后清除隐藏定时器clearTimeout(clearIconHideTimer.value[index]);}},},{ default: () => h(CircleClose) },);const inputNode = h('p',{contenteditable: !itemConf.value.baseConf.isReadOnly,class: ['underline_input',{'is-disabled': itemConf.value.baseConf.isReadOnly,},],disabled: itemConf.value.baseConf.isReadOnly,innerHTML: inputValues.value[index],placeholder: unref(itemConf).customConf?.inputGroup[index]?.placeholder || '请输入',onInput: async (event: InputEvent) => {const target = event.target as HTMLParagraphElement;adjustInputWidth(index);inputValues.value[index] = target.innerHTML;await nextTick(() => {moveCursorToEnd(target);});},onFocus: () => {if (!itemConf.value.baseConf.isReadOnly) {isInputFocused.value[index] = true;clearTimeout(clearIconHideTimer.value[index]);}},onBlur: () => {if (!itemConf.value.baseConf.isReadOnly) {handleChange(itemConf.value.customConf.inputGroup[index], inputValues.value[index]);clearIconHideTimer.value[index] = setTimeout(() => {if (!isClearIconClicked.value[index]) {isInputFocused.value[index] = false;}isClearIconClicked.value[index] = false;}, 200);}},onMousedown: (event: MouseEvent) => {if (itemConf.value.baseConf.isReadOnly) {event.preventDefault();event.stopPropagation();}},onKeydown: (event: KeyboardEvent) => {if (itemConf.value.baseConf.isReadOnly) {event.preventDefault();event.stopPropagation();}},ref: (el) => (inputRefs.value[index] = el),},// [clearIcon],);nodes.push(h('p', { class: 'underline_input_wrap' }, [inputNode, clearIcon]));}});return h('div', nodes);
});

css部分


.underline_input_wrap {display: inline-block;// max-width: calc(100% - 70px);position: relative;margin-top: 20px;margin-bottom: 0;max-width: calc(100% - 50px);
}
.underline_input {position: relative;height: 40px;min-width: 101px;// max-width: calc(100% - 70px);max-width: 100%;background: #f5f7fb;border-radius: 6px 6px 6px 6px;border: none;margin-left: 10px;margin-top: 0;margin-bottom: 0;display: inline-block;box-sizing: border-box;padding: 0 26px 0 12px;background: #f5f7fb;vertical-align: middle;color: #606266;background: #f5f7fb;vertical-align: middle;&:focus {outline: none;border: 1px solid #1a77ff;color: #606266;}&:disabled {color: #bbbfc4;cursor: not-allowed;}&::placeholder {color: #a8abb2;font-size: 14px;}
}.underline_input.is-disabled {color: #bbbfc4;cursor: not-allowed;
}.underline_input[contenteditable='true']:empty::before,
.underline_input.is-disabled:empty::before {content: attr(placeholder);color: #bbbfc4;
}
:deep(.clear_icon) {position: absolute;width: 14px;height: 14px;right: 5px;top: 50%;transform: translateY(-50%);cursor: pointer;color: #999;z-index: 10; /* 增加 z-index 确保在最上层 */&:hover {color: #666;}&.is-hidden {display: none;}
}

我们要模拟input可清除,所以需要我们去调整样式,以及placeholder样式问题

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/73959.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【AI】AI编程助手:Cursor、Codeium、GitHub Copilot、Roo Cline、Tabnine

文章目录 一、基本特性对比二、收费标准三、私有部署能力1、Tabnine2、Roo Code 三、代码补全与自然语言生成代码四、安装独立的IDE安装插件安装 五、基本使用&#xff08;一&#xff09;Cursor&#xff08;二&#xff09;GitHub Copilot1、获取代码建议2.聊天1&#xff09;上下…

三轴云台之角速度信号篇

三轴云台的角速度信号主要通过其内置的传感器&#xff08;如陀螺仪&#xff09;来感知和测量。 一、角速度信号的感知与测量 在三轴云台中&#xff0c;陀螺仪是测量角速度的关键组件。它通常安装在三个互相垂直的轴上&#xff08;通常为X、Y、Z轴&#xff09;&#xff0c;能够…

Grid 布局实现三栏布局

使用 CSS Grid 布局实现三栏布局(左右固定 100px,中间自适应)的核心原理是通过网格模板精确控制列宽分配。以下是具体实现方法及优化技巧: 一、基础实现 ​父容器设置 为外层容器添加 display: grid 使其成为网格容器,并通过 grid-template-columns 定义列宽 css .contain…

绿盟春招实习一面

《网安面试指南》https://mp.weixin.qq.com/s/RIVYDmxI9g_TgGrpbdDKtA?token1860256701&langzh_CN 5000篇网安资料库https://mp.weixin.qq.com/s?__bizMzkwNjY1Mzc0Nw&mid2247486065&idx2&snb30ade8200e842743339d428f414475e&chksmc0e4732df793fa3bf39…

进制转换(R转十)(1290. 二进制转换十进制、1292. 十六进制转十进制、1291. 八进制转十进制、1405. 小丽找潜在的素数)

题单地址&#xff1a;题单中心-东方博宜OJ 这里以二进制转十进制为例&#xff08;按位加权求和法&#xff09; 1290. 二进制转换十进制 问题描述 请将一个 25 位以内的 2 进制正整数转换为 1010 进制&#xff01; 输入 一个 25 位以内的二进制正整数。 输出 该数对应的…

Redis 本地安装

首先安装&#xff1a; https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/install-redis-from-source/ 进入root目录 tar -xzvf redis-stable.tar.gz cd redis-stable make然后 install sudo make install最后可以直接启动 redis-server但是此时启…

9.嗅探与Wireshark进阶分析

嗅探与Wireshark进阶分析 第一部分&#xff1a;嗅探的概念与重要性第二部分&#xff1a;Wireshark进阶功能第三部分&#xff1a;嗅探实践与分析总结 目标&#xff1a; • 理解嗅探&#xff08;Sniffing&#xff09;的概念及其在网络安全中的作用 • 掌握Wireshark的进阶功能&a…

在 VSCode 远程开发环境下使用 Git 常用命令

在日常开发过程中&#xff0c;无论是单人项目还是团队协作&#xff0c;Git 都是版本管理的利器。尤其是在使用 VSCode 连接远程服务器进行代码开发时&#xff0c;Git 不仅能帮助你管理代码版本&#xff0c;还能让多人协作变得更加高效。本文将介绍一些常用的 Git 命令&#xff…

npm 命令使用文档

目录 简介安装与配置基础命令依赖管理版本控制脚本管理包发布高级命令配置管理最佳实践常见问题 1. 简介 npm (Node Package Manager) 是 Node.js 的官方包管理工具&#xff0c;提供&#xff1a; 130万 开源包的注册表访问依赖解析与版本管理项目脚本自动化私有包管理能力完…

【Linux篇】进程控制

&#x1f4cc; 个人主页&#xff1a; 孙同学_ &#x1f527; 文章专栏&#xff1a;Liunx &#x1f4a1; 关注我&#xff0c;分享经验&#xff0c;助你少走弯路&#xff01; 1. 进程创建 1.1 fork函数 在linux中fork函数是非常重要的函数&#xff0c;它从已存在进程中创建一个…

HyperAD:学习弱监督音视频暴力检测在双曲空间中的方法

文章目录 速览摘要1. 引言2. 相关工作弱监督暴力检测双曲空间中的神经网络 3. 预备知识双曲几何切空间&#xff08;Tangent Space&#xff09;指数映射与对数映射&#xff08;Exponential and Logarithmic Maps&#xff09;3.1 双曲图卷积网络&#xff08;Hyperbolic Graph Con…

动态规划(6.不同路径II)

题目链接&#xff1a;63. 不同路径 II - 力扣&#xff08;LeetCode&#xff09; 解法&#xff1a; 本题为不同路径的变型&#xff0c;只不过有些地方有「障碍物」&#xff0c;只要在「状态转移」上稍加修改就可解决。 状态表示&#xff1a; 对于这种Γ路径类」的问题&#xf…

深度洞察:DeepSeek 驱动金融行业智能化转型变革

该文章为软件测评&#xff0c;不是广告&#xff01;&#xff01;&#xff01;&#xff01; 目录 一.金融行业的智能化转型浪潮​ 二.DeepSeek的核心技术剖析 1.DeepSeek 模型的金融智慧​ 2.实时联网搜索&#xff1a;把握金融市场脉搏​ 3.RAG 能力&#xff1a;铸就精准金…

蓝桥杯备考----》暴力枚举---金盏花

这道题&#xff0c;一共12位&#xff0c;给了后六位&#xff0c;我们只要枚举前六位就行了&#xff0c;当然如果是10的12次方的话&#xff0c;必须要开long long才可以存下&#xff0c;这点我们不要忘了 然后题目中又告诉了没有前导0&#xff0c;我们可以从100000开始枚举&…

RAG各类方法python源码解读与实践:利用Jupyter对RAG技术综合评测【3万字长文】

检索增强生成&#xff08;RAG &#xff09;是一种结合信息检索与生成模型的混合方法。它通过引入外部知识来提升语言模型的性能&#xff0c;从而提高回答的准确性和事实正确性。为了简单易学&#xff0c;不使用LangChain框架或FAISS向量数据库&#xff0c;而是利用Jupyter Note…

Python列表2

print("—————————— 列表的相关操作 ————————————")lst.append(x)在列表lst最后增加一个元素 lst.insert(index,x)在列表中第index位置增加一个元素 lst.clear()清除列表lst中所有元素 lst.pop(index)将列表lst中第index位置的元素取出&#xf…

华为OD机试-IPv4地址转换成整数(Java 2024 B卷 100分)

题目描述 存在一种虚拟 IPv4 地址 Q,由 4 小节组成,每节的范围为 0~255,以 # 号间隔。虚拟 IPv4 地址可以转换为一个 32 位的整数。例如: 128#0#255#255 转换为 32 位整数的结果为 2147549183(0x8000FFFF)1#0#0#0 转换为 32 位整数的结果为 16777216(0x01000000)现以字…

C语言复习笔记--数组

今天继续来浅浅推进一下C语言的复习,这次是数组的复习,话不多说,正文开始. 数组的概念 数组是⼀组相同类型元素的集合,一种自定义类型.数组中元素个数不能为0.数组分为⼀维数组和多维数组&#xff0c;多维数组⼀般⽐较多⻅的是⼆维数组. 下面从一维数组说起. 一维数组的创建和…

Canal 解析与 Spring Boot 整合实战

一、Canal 简介 1.1 Canal 是什么&#xff1f; Canal 是阿里巴巴开源的一款基于 MySQL 数据库增量日志解析&#xff08;Binlog&#xff09;中间件&#xff0c;它模拟 MySQL 的从机&#xff08;Slave&#xff09;行为&#xff0c;监听 MySQL 主机的二进制日志&#xff08;Binl…

《论语别裁》第01章 学而(31) 诗的人生

不过这句话研究起来有一个问题&#xff0c;是诗的问题。我们知道中国文化&#xff0c;在文学的境界上&#xff0c;有一个演变发展的程序&#xff0c;大体的情形&#xff0c;是所谓汉文、唐诗、宋词、元曲、明小说&#xff0c;到了清朝&#xff0c;我认为是对联&#xff0c;尤其…