When div is set to contentEditable=true, the cursor cannot be positioned after resetting its content

When div is set to contentEditable=true, the cursor cannot be positioned after resetting its content

I was recently working on a comment feature that required the ability to comment on emoticons, so the contentEditable property was the first thing to consider. As a result, a problem arose...

First, comment on the district chief:

img

When the input content exceeds the limit, clear the content after the user input exceeds the limit.

This is easy... But after clearing the content, the cursor actually moved to the front, which is a headache.

After various searches, I found a solution. Without further ado, here is the code!

var _div = document.querySelector('.discuss_area');
var range = document.createRange();

range.selectNodeContents(_div);
range.collapse(false);

var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

All of them use native events, so jQuery objects cannot be used.

This article is about solving the problem that when a div is set to contentEditable=true, the cursor cannot be positioned correctly after resetting its content. This is the end of the article. For more relevant content about div setting contentEditable=true, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Common naming rules for CSS classes and ids

>>:  Detailed explanation of CSS background and border tag examples

Recommend

Detailed explanation of the use of $emit in Vue.js

1. Parent components can use props to pass data t...

Encoding problems and solutions when mysql associates two tables

When Mysql associates two tables, an error messag...

Lombok implementation JSR-269

Preface Introduction Lombok is a handy tool, just...

How to modify the time zone and time in Ubuntu system

On a Linux computer, there are two times, one is ...

How to execute Linux shell commands in Docker

To execute a shell command in Docker, you need to...

Docker installs ClickHouse and initializes data testing

Clickhouse Introduction ClickHouse is a column-or...

How to install and deploy gitlab server on centos7

I am using centos 7 64bit system here. I have tri...

CSS3 property line-clamp controls the use of text lines

Description: Limit the number of lines of text di...

Centos7.5 installs mysql5.7.24 binary package deployment

1. Environmental preparation: Operating system: C...

Implementation code for partial refresh of HTML page

Event response refresh: refresh only when request...

Comprehensive understanding of html.css overflow

Comprehensive understanding of html.css overflow ...

Three methods of automatically completing commands in MySQL database

Note: The third method is only used in XSell and ...

Promise encapsulation wx.request method

The previous article introduced the implementatio...

JavaScript implements a box that follows the mouse movement

This article shares the specific code of JavaScri...