码迷,mamicode.com
首页 > 其他好文 > 详细

获取输入框光标位置及指定位置插入内容

时间:2020-09-18 03:20:13      阅读:37      评论:0      收藏:0      [点我收藏+]

标签:光标位置   length   extra   ext   cron   html   tar   his   ndt   

<!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>
<textarea name="" id="edit" cols="100" rows="10"></textarea>
<button οnclick="getPos()">获取光标位置</button>
<button οnclick="getSelect()">获取选中内容</button>
<button οnclick="insert()">插入文本</button>
<script>
    $.extend($.fn,{
        //获取文本框内光标位置
        getSelectionStart: function() {
            var e = this[0];
            if (e.selectionStart) {
                return e.selectionStart;
            } else if (document.selection) {
                e.focus();
                var r=document.selection.createRange();
                var sr = r.duplicate();
                sr.moveToElementText(e);
                sr.setEndPoint(EndToEnd, r);
                return sr.text.length - r.text.length;
            }
 
            return 0;
        },
        getSelectionEnd: function() {
            var e = this[0];
            if (e.selectionEnd) {
                return e.selectionEnd;
            } else if (document.selection) {
                e.focus();
                var r=document.selection.createRange();
                var sr = r.duplicate();
                sr.moveToElementText(e);
                sr.setEndPoint(EndToEnd, r);
                return sr.text.length;
            }
            return 0;
        },
        //自动插入默认字符串
        insertString: function(str) {
            $(this).each(function() {
                var tb = $(this);
                tb.focus();
                if (document.selection){
                    var r = document.selection.createRange();
                    document.selection.empty();
                    r.text = str;
                    r.collapse();
                    r.select();
                } else {
                    var newstart = tb.get(0).selectionStart+str.length;
                    tb.val(tb.val().substr(0,tb.get(0).selectionStart) +
                            str + tb.val().substring(tb.get(0).selectionEnd));
                    tb.get(0).selectionStart = newstart;
                    tb.get(0).selectionEnd = newstart;
                }
            });
 
            return this;
        },
        setSelection: function(startIndex,len) {
            $(this).each(function(){
                if (this.setSelectionRange){
                    this.setSelectionRange(startIndex, startIndex + len);
                } else if (document.selection) {
                    var range = this.createTextRange();
                    range.collapse(true);
                    range.moveStart(character, startIndex);
                    range.moveEnd(character, len);
                    range.select();
                } else {
                    this.selectionStart = startIndex;
                    this.selectionEnd = startIndex + len;
                }
            });
 
            return this;
        },
        getSelection: function() {
            var elem = this[0];
 
            var sel = ‘‘;
            if (document.selection){
                var r = document.selection.createRange();
                document.selection.empty();
                sel = r.text;
            } else {
                var start = elem.selectionStart;
                var end = elem.selectionEnd;
                var content = $(elem).is(:input) ? $(elem).val() : $(elem).text();
                sel = content.substring(start, end);
            }
            return sel;
        }
    })
</script>
<script>
    function getPos(){
        alert($("#edit").getSelectionStart());
    }
    function getSelect(){
        alert($("#edit").getSelection());
    }
    function insert(){
       $("#edit").insertString("hello");
 
    }
</script>
</body>
</html>

 

获取输入框光标位置及指定位置插入内容

标签:光标位置   length   extra   ext   cron   html   tar   his   ndt   

原文地址:https://www.cnblogs.com/wangyongx/p/13680619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!