当前位置:网站首页>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
边栏推荐
- 闭包实现迭代器效果
- Day06 branch structure and cycle (III)
- 队列的实现和应用
- The fixed assets management system enables enterprises to dynamically master assets
- Pain points and solutions of fixed assets management of group companies
- Flink面试题
- I use flask to write the website "one"
- Databinding source code analysis
- es6-顶层对象与window的脱钩
- js this丢失问题分析 及 解决方案
猜你喜欢

Pain points and solutions of fixed assets management of group companies
![[pytorch] softmax function](/img/97/b8ae22e8496a77e665d716cb0e9ee3.png)
[pytorch] softmax function

Principles of Microcomputer - Introduction

Implementation and application of queue

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

OSPF - virtual link details (including configuration commands)

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

Principles of Microcomputer - internal and external structure of microprocessor

MySQL optimization

JS原型链
随机推荐
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
js原型陷阱
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
Nacos service configuration and persistence configuration
Installation and use of NoSQL database
Principles of Microcomputer - Introduction
Shell script -select in loop
js作用域链与闭包
Differences between JS valueof and toString
2.2 【pytorch】torchvision.transforms
Flink面试题
nacos简易实现负载均衡
Football and basketball game score live broadcast platform source code /app development and construction project
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
Shell script case in statement
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
Jetson Nano 安装TensorFlow GPU及问题解决
2022.02.15_ Daily question leetcode six hundred and ninety
短路运算符惰性求值
An overview of the design of royalties and service fees of mainstream NFT market platforms