javastscript获取光标位置

需求是获取某元素的内容,然后将该内容插入到文本框当前的光标位置

 (function($) {
                $.fn.extend({
                    insertAtCaret: function(myValue) {
                        var $t = $(this)[0];
                        if (document.selection) {
                            this.focus();
                            sel = document.selection.createRange();
                            sel.text = myValue;
                            this.focus();
                        }
                        else
                        if ($t.selectionStart || $t.selectionStart == ‘0‘) {
                            var startPos = $t.selectionStart;
                            var endPos = $t.selectionEnd;
                            var scrollTop = $t.scrollTop;
                            $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
                            this.focus();
                            $t.selectionStart = startPos + myValue.length;
                            $t.selectionEnd = startPos + myValue.length;
                            $t.scrollTop = scrollTop;
                        }
                        else {
                            this.value += myValue;
                            this.focus();
                        }
                    }
                })
            })(jQuery);

使用方法

$(‘#demo‘).insertAtCaret(‘hello‘);
时间: 2024-11-08 13:15:52

javastscript获取光标位置的相关文章

android EditText获取光标位置并安插字符删除字符

android EditText获取光标位置并插入字符删除字符1.获取光标位置int index = editText.getSelectionStart(); 2.在光标处插入字符int index = editText.getSelectionStart();Editable editable = editText.getText();editable.insert(index, "aaaa");3.删除光标前字符int index = editText.getSelectionS

关于文本输入框获取光标位置以及指定位置插入内容

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> </head> <body> &l

android EditText获取光标位置并且插入字符或者删除字符

1.获取光标位置 [java] int index = editText.getSelectionStart(); int index = editText.getSelectionStart(); 2.在光标处插入字符 [java] int index = editText.getSelectionStart(); Editable editable = editText.getText(); editable.insert(index, "aaaa"); int index = e

获取光标位置

原文:https://blog.csdn.net/mafan121/article/details/78519348 html <iframe id="editor" width="600px" height="400px" style="border:solid 1px;"></iframe> <input type="txt" onclick="alert(get

可编辑div 获取光标位置及插入内容

<!DOCTYPE html> <html> <title>Div 光标位置 插入 文字 或 HTML</title> <script language="javascript" type="text/javascript"> var range; function getPos() { range = window.getSelection().getRangeAt(0);//找到焦点位置 var spa

UITextView获取光标位置

UITextRange *range = textView.selectedTextRange; //光标位置 CGRect  rect = [textView caretRectForPosition:range.start]; 当UITextView的光标在最后位置连续输入4个或以上换行符号时,输出rect为 {{inf,inf},{0,0}}

Android EditText获取光标位置并插入字符删除字符

1.获取到光标所在的位置 int index = editText.getSelectionStart(); 2.在光标的地方插入字符 int index = editText.getSelectionStart(); Editable editable = editText.getText(); editable.insert(index, "哈哈哈"); 3.删除光标前的字符(光标在哪里从哪里开始删除) int index = editText.getSelectionStart(

jquery扩展获取光标位置

(function($){ $.fn.extend({ insertAtCaret: function(myValue){ var $t=$(this)[0]; if (document.selection) { this.focus(); sel = document.selection.createRange(); sel.text = myValue; this.focus(); } else if ($t.selectionStart || $t.selectionStart == '0

javascript获取以及设置光标位置

一. 获取光标位置: // 获取光标位置 function getCursortPosition (textDom) { var cursorPos = 0; if (document.selection) { // IE Support textDom.focus (); var selectRange = document.selection.createRange(); selectRange.moveStart ('character', -textDom.value.length);