• js 光标选中 操作


    <!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> 
    

      

  • 相关阅读:
    Linux启动网卡时出现RTNETLINK answers: File exists错误解决方法
    在Linux系统中应用su和sudo
    Python模块常用的几种安装方式
    安装php并使用nginx连接PHP
    查看Chrome浏览器扩展程序源码的两种方法
    angularJs中缓存数据,免去重复发起请求的几种写法
    underscore.js 分析6 map函数
    AngularJS中Directive指令系列
    $watch和$observe的使用
    $watch, $watchCollection, $watchGroup的使用
  • 原文地址:https://www.cnblogs.com/JamyWong/p/7642318.html
Copyright © 2020-2023  润新知