This article describes the cursor position tool function implemented by js. Share it for your reference, as follows:
An example of the cursor position tool function in a textarea introduced here.
html code:
<p>Text box:</p> <textarea name="" cols="30" rows="10"> sASASADASDasfaDFDsfsDFAfdFADf </textarea> <button type="button" >Select a specific range of text</button> <button type="button" >Insert text after cursor</button> <br> <hr> <br> <input type="number" name="" > <button type="button" >Set cursor position</button> <br> <hr> <br> <p >I'm a text that can be selected,Please open the console to view。I'm a text that can be selected,Please open the console to view。I'm a text that can be selected,Please open the console to view。</p>
js code:
/** * Cursor position component * ## method of selectRange object: * (1)(ele) [Create a new object obtained by cursor position] parameter: ele {{JavaScript DOM}} The element where the cursor is located, native JavaScript DOM * (2)() [Get the current coordinate position] * (3)(pos) [Set the current cursor position] parameter: pos {{Int}} Current cursor position * (4)() [Get selected text] * (5)(startPos, endPos) [Select text with a specific range (only textarea and input)] * Parameters: startPos {{Int}} Start Pos endPos {{Int}} End Position * (6)(value) [Insert text after cursor] * Parameters: value {{String}} Text to be inserted * * * ## Examples: * (EleDom).getCurPos(); // Get the current coordinate position * (EleDom).setCurPos(pos); // Set the current cursor position to pos * (EleDom).getSelectText(); // Get selected text * (EleDom).setSelectText(startPos, endPos); // Select text from the range startPos to endPos * (EleDom).insertAfterText(value); // Insert text with value after the cursor */ var selectRange = function(ele){ this.__element = ele; }; // Create a new object to obtain the cursor position = function(ele){ if(ele) { return new selectRange(ele); } else { return {}; } }; = { constructor:selectRange, // Get the current coordinate position getCurPos: function() { var _this = this, textDom = _this.__element; // Get the cursor position var cursorPos = 0; if () { // IE Support (); var selectRange = (); ('character', -); cursorPos = ; }else if ( || == '0') { // Firefox support cursorPos = ; } return cursorPos; }, /** * Set the current cursor position * Parameters: * pos [Int] Current cursor position */ setCurPos: function(pos) { var _this = this, textDom = _this.__element; if() { // IE Support (); (pos, pos); }else if () { // Firefox support var range = (); (true); ('character', pos); ('character', pos); (); } }, // Get selected text getSelectText: function() { var _this = this, textDom = _this.__element, userSelection, text = ""; if () { // Firefox support userSelection = (); } else if () { // IE Support userSelection = (); } if (!(text = )) { text = userSelection; } return text; }, /** * Select text with a specific range (only textarea and input) * Parameters: * startPos [Int] Start Pos * endPos [Int] End Position */ setSelectText: function(startPos, endPos) { var _this = this, textDom = _this.__element, startPos = parseInt(startPos), endPos = parseInt(endPos), textLength = ; if(textLength){ if(!startPos){ startPos = 0; } if(!endPos){ endPos = textLength; } if(startPos > textLength){ startPos = textLength; } if(endPos > textLength){ endPos = textLength; } if(startPos < 0){ startPos = textLength + startPos; } if(endPos < 0){ endPos = textLength + endPos; } if(){ // IE Support var range = (); ("character",-textLength); ("character",-textLength); ("character", startPos); ("character",endPos); (); }else{ // Firefox support (startPos, endPos); (); } } }, /** * Insert text after cursor * Parameters: * value [String] Text to be inserted */ insertAfterText: function(value) { var _this = this, textDom = _this.__element, selectRange; if () { // IE Support (); selectRange = (); = value; (); }else if ( || == '0') { // Firefox support var startPos = ; var endPos = ; var scrollTop = ; = (0, startPos) + value + (endPos, ); (); = startPos + ; = startPos + ; = scrollTop; } else { += value; (); } } }; // =============================================== // Instance codevar textareaDom = ("#textarea"), setCurPosInput = ("#input"), setCurPosBtn = ("#setCurPosBtn"), selectText = ("#selectText"), setSelectTextBtn = ("#setSelectText"), insertAfterTextBtn = ("#insertAfterText"); // Get the current cursor position("keydown", function() { ("getCurPos: " + (textareaDom).getCurPos()); }, false); // Set the current cursor position("click", function() { var curPos = parseInt(); ("curPos: " + curPos); (textareaDom).setCurPos(curPos); }, false); // Get selected text("mouseup", function() { ("selectText: " + (this).getSelectText()); }); // Select text in a specific range("click", function() { (textareaDom).setSelectText(0, 21); }, false); // Insert text after cursor("click", function() { var insertText = "===hello world, I'm insert content.==="; (textareaDom).insertAfterText(insertText); }, false);
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of json operation skills in JavaScript》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript traversal algorithm and skills"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.