当前位置:网站首页>富文本实现插值
富文本实现插值
2022-07-01 09:20:00 【tianming2018】
最近项目上需要一个富文本框实现插值操作,插值不可编辑,但是可以删除,尝试了好几种方案,最终决定用contenteditable这个属性来实现
首先, contenteditable="true"
可以使元素成为可编辑状态
<div contenteditable="true"></div>
在设置了contenteditable属性的元素中,可以插入或编辑任意的元素,如果要实现不可编辑的插值的话,可以将插值的元素设置为contenteditable="false"
,这样,就实现了富文本插值。
但是,还有问题,如何将元素插入指定的光标位置
浏览器提供了selection和range对象来操作光标
// 将内容插入到光标位置,并将光标移动到内容元素末尾
function insertAtCursor(domNode) {
// 获取被选中的内容,起点和终点在同一位置为光标,不同位置为选区
const selection = window.getSelection();
// 被选中/focus的元素
const anchorNode = selection.anchorNode;
if (!anchorNode) return;
// 父节点
const range = selection.getRangeAt(0);
// 插入传入的元素节点
range.insertNode(domNode);
// collapse为true使起点和终点位于同一位置,即光标
// false时则为选区内容
range.collapse(true);
// 光标移到最后
const r = document.createRange();
r.selectNodeContents(domNode);
r.collapse(false);
const s = window.getSelection();
s.removeAllRanges();
s.addRange(r);
}
这样,就可以把元素插入光标位置,并把光标移动到元素末尾
边栏推荐
- Exception handling of classes in C #
- Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
- 集成积木报表报错 org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常
- Serialization, listening, custom annotation
- 【pytorch】2.4 卷积函数 nn.conv2d
- Nacos service configuration and persistence configuration
- JS variable lifting
- js原型继承仅可继承实例而非构造器
- Short circuit operator lazy evaluation
- [ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
猜你喜欢
Simple load balancing with Nacos
[pytorch] 2.4 convolution function nn conv2d
Learning practice: comprehensive application of cycle and branch structure (II)
dsPIC30F6014a LCD 方块显示
JS原型链
nacos簡易實現負載均衡
An overview of the design of royalties and service fees of mainstream NFT market platforms
树结构---二叉树2非递归遍历
How to solve the problem of fixed assets management and inventory?
【pytorch】softmax函数
随机推荐
tensorrt yolov5_ trt. Py comments
JS scope chain and closure
js重写自己的函数
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
Leetcode daily question brushing record --540 A single element in an ordered array
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
3D printing Arduino four axis aircraft
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
Flink面试题
R language observation log (part24) -- initialization settings
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
Short circuit operator lazy evaluation
树结构---二叉树1
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
Shell script - special variables: shell $, $*, [email protected], $$$
Imitation of Baidu search results top navigation bar effect
How to launch circle of friends marketing and wechat group activities
计网01-物理层