当前位置:网站首页>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.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data
- JS functionarguments object
- Flink interview questions
- 利用闭包实现私有变量
- Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
- JS prototype inheritance can only inherit instances, not constructors
- [interview brush 101] linked list
- laravel postman 提交表单出现419错误。2020年7月6日记。
- Leetcode daily question brushing record --540 A single element in an ordered array
猜你喜欢

小鸟识别APP

MapReduce编程基础

Learning practice: comprehensive application of cycle and branch structure (II)
![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)
[interview brush 101] linked list

Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month

钓鱼识别app

【pytorch】nn. Crossentropyloss() and nn NLLLoss()

Structure de l'arbre - - - arbre binaire 2 traversée non récursive

MySQL optimization

2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
随机推荐
Shell script - array definition and getting array elements
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
Implementation and application of queue
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
js重写自己的函数
Simple load balancing with Nacos
Daily practice of C language - day 80: currency change
Day06 branch structure and cycle (III)
Differences between JS valueof and toString
2.2 【pytorch】torchvision. transforms
js this丢失问题分析 及 解决方案
JS rewrite their own functions
Shell script -select in loop
MT7628K eCos开发入门
【pytorch】nn.AdaptiveMaxPool2d
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
Mise en œuvre simple de l'équilibrage de la charge par nacos
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
美团2022年机试
An overview of the design of royalties and service fees of mainstream NFT market platforms