文本域光標操作(選、添、刪、取)的jQuery擴展


; (function ($) {
            /*
             * 文本域光標操作(選、添、刪、取)的jQuery擴展 @Mr.Think http://mrthink.net/text-field-jquery-extend/
             */
            $.fn.extend({
                /*
                 * 獲取光標所在位置
                 */
                iGetFieldPos: function () {
                    var field = this.get(0);
                    if (document.selection) {
                        //IE
                        $(this).focus();
                        var sel = document.selection;
                        var range = sel.createRange();
                        var dupRange = range.duplicate();
                        dupRange.moveToElementText(field);
                        dupRange.setEndPoint('EndToEnd', range);
                        field.selectionStart = dupRange.text.length - range.text.length;
                        field.selectionEnd = field.selectionStart + range.text.length;
                    }
                    return field.selectionStart;
                },
                /*
                 * 選中指定位置內字符 || 設置光標位置
                 * --- 從start起選中(含第start個),到第end結束(不含第end個)
                 * --- 若不輸入end值,即為設置光標的位置(第start字符后)
                 */
                iSelectField: function (start, end) {
                    var field = this.get(0);
                    //end未定義,則為設置光標位置
                    if (arguments[1] == undefined) {
                        end = start;
                    }
                    if (document.selection) {
                        //IE
                        var range = field.createTextRange();
                        range.moveEnd('character', -$(this).val().length);
                        range.moveEnd('character', end);
                        range.moveStart('character', start);
                        range.select();
                    } else {
                        //非IE
                        field.setSelectionRange(start, end);
                        $(this).focus();
                    }
                },
                /*
                 * 選中指定字符串
                 */
                iSelectStr: function (str) {
                    var field = this.get(0);
                    var i = $(this).val().indexOf(str);
                    i != -1 ? $(this).iSelectField(i, i + str.length) : false;
                },
                /*
                 * 在光標之后插入字符串
                 */
                iAddField: function (str) {
                    var field = this.get(0);
                    var v = $(this).val();
                    var len = $(this).val().length;
                    if (document.selection) {
                        //IE
                        $(this).focus()
                        document.selection.createRange().text = str;
                    } else {
                        //非IE
                        var selPos = field.selectionStart;
                        $(this).val($(this).val().slice(0, field.selectionStart) + str + $(this).val().slice(field.selectionStart, len));
                        this.iSelectField(selPos + str.length);
                    };
                },
                /*
                 * 刪除光標前面(-)或者后面(+)的n個字符
                 */
                iDelField: function (n) {
                    var field = this.get(0);
                    var pos = $(this).iGetFieldPos();
                    var v = $(this).val();
                    //大於0則刪除后面,小於0則刪除前面
                    if(pos>0){
             $(this).val(n > 0 ? v.slice(0, pos - n) + v.slice(pos) : v.slice(0, pos) + v.slice(pos - n));   $(this).iSelectField(pos - (n < 0 ? 0 : n));
            } } }); })(jQuery);

應用場景

<!DOCTYPE html>
<html>
<head>
    <title>無標題頁</title>

    <script type="text/javascript" src="http://sy.zgsapt.com/js/jquery-1.7.2.min.js"></script>

    <script type="text/javascript">
        ; (function ($) {
            /*
             * 文本域光標操作(選、添、刪、取)的jQuery擴展 @Mr.Think http://mrthink.net/text-field-jquery-extend/
             */
            $.fn.extend({
                /*
                 * 獲取光標所在位置
                 */
                iGetFieldPos: function () {
                    var field = this.get(0);
                    if (document.selection) {
                        //IE
                        $(this).focus();
                        var sel = document.selection;
                        var range = sel.createRange();
                        var dupRange = range.duplicate();
                        dupRange.moveToElementText(field);
                        dupRange.setEndPoint('EndToEnd', range);
                        field.selectionStart = dupRange.text.length - range.text.length;
                        field.selectionEnd = field.selectionStart + range.text.length;
                    }
                    return field.selectionStart;
                },
                /*
                 * 選中指定位置內字符 || 設置光標位置
                 * --- 從start起選中(含第start個),到第end結束(不含第end個)
                 * --- 若不輸入end值,即為設置光標的位置(第start字符后)
                 */
                iSelectField: function (start, end) {
                    var field = this.get(0);
                    //end未定義,則為設置光標位置
                    if (arguments[1] == undefined) {
                        end = start;
                    }
                    if (document.selection) {
                        //IE
                        var range = field.createTextRange();
                        range.moveEnd('character', -$(this).val().length);
                        range.moveEnd('character', end);
                        range.moveStart('character', start);
                        range.select();
                    } else {
                        //非IE
                        field.setSelectionRange(start, end);
                        $(this).focus();
                    }
                },
                /*
                 * 選中指定字符串
                 */
                iSelectStr: function (str) {
                    var field = this.get(0);
                    var i = $(this).val().indexOf(str);
                    i != -1 ? $(this).iSelectField(i, i + str.length) : false;
                },
                /*
                 * 在光標之后插入字符串
                 */
                iAddField: function (str) {
                    var field = this.get(0);
                    var v = $(this).val();
                    var len = $(this).val().length;
                    if (document.selection) {
                        //IE
                        $(this).focus()
                        document.selection.createRange().text = str;
                    } else {
                        //非IE
                        var selPos = field.selectionStart;
                        $(this).val($(this).val().slice(0, field.selectionStart) + str + $(this).val().slice(field.selectionStart, len));
                        this.iSelectField(selPos + str.length);
                    };
                },
                /*
                 * 刪除光標前面(-)或者后面(+)的n個字符
                 */
                iDelField: function (n) {
                    var field = this.get(0);
                    var pos = $(this).iGetFieldPos();
                    var v = $(this).val();
                    //大於0則刪除后面,小於0則刪除前面
                    $(this).val(n > 0 ? v.slice(0, pos - n) + v.slice(pos) : v.slice(0, pos) + v.slice(pos - n));
                    $(this).iSelectField(pos - (n < 0 ? 0 : n));
                }
            });
        })(jQuery);


        $(document).ready(function () {
            $("#numd").bind("mouseleave", function () {
                document.getElementById('keybored').style.display = 'none';
                document.getElementById('Nm').blur();
            });

            $("#Nm").focus(function () {
                document.getElementById('keybored').style.display = '';
            });
            $(".readbtns").click(function () {
                $("#Nm").iAddField($(this).val());

            });
            $(".returns").click(function () {
                $("#Nm").iDelField(1);

            });
        });

    </script>

</head>
<body>
    <ul>
        <li>
            <input />
            <div>
            </div>
        </li>
    </ul>
    <input id="hid" type="text" value="" style="display: none" />
    <span id="numd" style="border: 1px solid red; clear: both; display: inline-block; font: 800em;">
        <input type="text" id="Nm" name="Nm" value="" />
        <div style="display: none; border: 1px solid #A2B4C6; width: 150px; height: 400px;"
            id="keybored">
            <input type="button" class="readbtns" value="1" />
            <input type="button" class="readbtns" value="2" />
            <input type="button" class="readbtns" value="3" />
            <input type="button" class="readbtns" value="4" />
            <input type="button" class="readbtns" value="5" />
            <input type="button" class="readbtns" value="6" />
            <input type="button" class="readbtns" value="7" />
            <input type="button" class="readbtns" value="8" />
            <input type="button" class="readbtns" value="9" />
            <input type="button" class="returns" value="<<---" />
        </div>
    </span>
</body>
</html>

 經過查閱jQuery的hover

$('#hover_div').hover(function()
{
/* something to do on mouseenter */
},
function()
{
/* something to do on mouseleave */
});

所以上面的bind("mouseleave")完全可以用hover函數

 $("#numd").hover(function () {
                document.getElementById('keybored').style.display = '';
            }, function () {
                document.getElementById('keybored').style.display = 'none';
                document.getElementById('Nm').blur();

            });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM