当前位置:网站首页>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
边栏推荐
- Daily practice of C language - day 80: currency change
- Phishing identification app
- SDN_ Simple summary
- js函数arguments对象
- js this丢失问题分析 及 解决方案
- Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
- Shell script -read command: read data entered from the keyboard
- 【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- Can diffusion models be regarded as an autoencoder?
- 记一次redis超时
猜你喜欢
【pytorch】2.4 卷积函数 nn.conv2d
Why is the Ltd independent station a Web3.0 website!
NiO zero copy
钓鱼识别app
Microcomputer principle - bus and its formation
2.4 激活函数
Implementation and application of queue
How to realize the usage of connecting multiple databases in idel
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
Nacos service configuration and persistence configuration
随机推荐
Design and manufacture of simple digital display electronic scale
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
Football and basketball game score live broadcast platform source code /app development and construction project
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
Databinding source code analysis
【电赛训练】红外光通信装置 2013年电赛真题
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
2.4 activation function
Understand shallow replication and deep replication through code examples
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
JS functionarguments object
JS rewrite their own functions
Network counting 01 physical layer
Bird recognition app
js valueOf 与 toString 区别
[pytorch] 2.4 convolution function nn conv2d
Short circuit operator lazy evaluation