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

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

时间:2015-08-28 17:52:04      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:js   javascript   html   

<!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 onclick="getPos()">获取光标位置</button>
<button onclick="getSelect()">获取选中内容</button>
<button onclick="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>

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:js   javascript   html   

原文地址:http://blog.csdn.net/hearain528/article/details/48052853

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