This article mainly introduces how JS inserts content at the cursor position in an editable div, which is implemented by js and jQuery respectively. It is a very practical special effect technique, for more information about how to insert content at a specified position in editable p in js, see the example in this article. Just like the editor we use, we will share it with you for your reference. The specific implementation method is as follows:
First, enable the edit mode for the DIV.
The Code is as follows:
Enable the p edit mode by setting contenteditable = true, so that the DIV can input the content just like the text box.
Don't talk about the topic anymore. The following describes how to obtain or set the cursor position.
Two steps:
① Obtain the cursor position in the DIV
② Change the cursor position
The Code is as follows:
Var cursor = 0; // cursor position
Document. onselectionchange = function (){
Var range = document. selection. createRange ();
Var srcele = range. parentElement (); // get the current element
Var copy = document. body. createTextRange ();
Copy. moveToElementText (srcele );
For (cursor = 0; copy. compareEndPoints ("StartToStart", range) <0; cursor ++ ){
Copy. moveStart ("character", 1); // Changes the cursor position. In fact, we are recording the number of cursor.
}
}
Bind a cursor change event to the document. Used to record the cursor position.
In this way, you can get the cursor position of the DIV.
The Code is as follows:
Function moveEnd (obj ){
LyTXT1.focus ();
Var r = document. selection. createRange ();
// Because the cursor is automatically moved from the current cursor (as if the text box is counted from 0 .) so we need to get the current cursor position, and then we can calculate the number of digits to move, so that we can move the cursor to the desired position.
R. moveStart ('character ', lyTXT1.innerText. length-cursor );
R. collapse (true );
R. select ();
}
Through the above, we can move the cursor in the DIV to the end.
A complete instance
The Code is as follows:
Insert characters
Function insertHtmlAtCaret (html ){
Var sel, range;
If (window. getSelection ){
// IE9 and non-IE
Sel = window. getSelection ();
If (sel. getRangeAt & sel. rangeCount ){
Range = sel. getRangeAt (0 );
Range. deleteContents ();
// Range. createContextualFragment () wocould be useful here but is
// Non-standard and not supported in all browsers (IE9, for one)
Var el = document. createElement ("p ");
El. innerHTML = html;
Var frag = document. createDocumentFragment (), node, lastNode;
While (node = el. firstChild )){
LastNode = frag. appendChild (node );
}
Range. insertNode (frag );
// Preserve the selection
If (lastNode ){
Range = range. cloneRange ();
Range. setStartAfter (lastNode );
Range. collapse (true );
Sel. removeAllRanges ();
Sel. addRange (range );
}
}
} Else if (document. selection & document. selection. type! = "Control "){
// IE <9
Document. selection. createRange (). pasteHTML (html );
}
}
Let's look at a jquery-based instance.
The Code is as follows: