当前位置:网站首页>Rich text interpolation
Rich text interpolation
2022-07-01 09:21:00 【tianming2018】
Recently, a rich text box is needed in the project to implement the interpolation operation , Interpolation is not editable , But you can delete , Several schemes have been tried , Finally decided to use contenteditable This property to implement
First , contenteditable="true" You can make elements editable
<div contenteditable="true"></div>
In setting up the contenteditable Attribute , You can insert or edit any element , If you want to realize non editable interpolation , You can set the interpolated element to contenteditable="false", such , It realizes rich text interpolation .
however , Do you have any questions , How to insert an element into a specified cursor position
The browser provides selection and range Object to manipulate the cursor
// Insert the content at the cursor position , And move the cursor to the end of the content element
function insertAtCursor(domNode) {
// Get the selected content , The starting point and the ending point are the cursor at the same position , Different locations are constituencies
const selection = window.getSelection();
// To be selected /focus The elements of
const anchorNode = selection.anchorNode;
if (!anchorNode) return;
// Parent node
const range = selection.getRangeAt(0);
// Insert the incoming element node
range.insertNode(domNode);
// collapse by true Make the starting point and the ending point at the same position , Cursor
// false When, it is the content of the selected area
range.collapse(true);
// Move the cursor to the last
const r = document.createRange();
r.selectNodeContents(domNode);
r.collapse(false);
const s = window.getSelection();
s.removeAllRanges();
s.addRange(r);
}
such , You can insert the element into the cursor position , And move the cursor to the end of the element
边栏推荐
- 2.2 【pytorch】torchvision.transforms
- nacos简易实现负载均衡
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
- 韦东山板子编译内核问题解决
- [pytorch] 2.4 convolution function nn conv2d
- Naoqi robot summary 28
- Understand shallow replication and deep replication through code examples
- 【pytorch】nn. Crossentropyloss() and nn NLLLoss()
- Niuke monthly race 22- collect pieces of paper
- 2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
猜你喜欢

Why is the Ltd independent station a Web3.0 website!

Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)

How to manage fixed assets efficiently in one stop?

樹結構---二叉樹2非遞歸遍曆

Preparing for the Blue Bridge Cup -- bit operation

Installation and use of NoSQL database

Microcomputer principle - bus and its formation

2.4 activation function
![delete和delete[]引发的问题](/img/d9/a1c3e5ce51ef1be366a973aa42d1f0.png)
delete和delete[]引发的问题

ESP8266 FreeRTOS开发环境搭建
随机推荐
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
Error org apache. catalina. core. StandardContext. FilterStart start filter exception
Jetson nano installs tensorflow GPU and problem solving
R language observation log (part24) -- initialization settings
短路运算符惰性求值
Shell script -if else statement
How to effectively align team cognition
韦东山板子编译内核问题解决
序列化、监听、自定义注解
phpexcel 里 获取某一列的列表 获取某一列的字母
ES6 decoupling top-level objects from windows
Serialization, listening, custom annotation
Mysql 优化
Pain points and solutions of fixed assets management of group companies
手指点击屏幕就模拟进入F11进入全屏
Class loading
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
[pytorch] softmax function
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警