DIV CSS 佈局教程網

用 jQuery 在游標位置插入內容的完整實作
編輯:JQuery特效代碼     

自製鍵盤、表情面板或快速標記按鈕時,都需要把字元精準插入到輸入框「目前游標所在的位置」,而不是附加在尾端。這件事在 IE 與其他瀏覽器的做法不同,所以得寫成插件包起來。

核心思路

在支援 selectionStart 的瀏覽器中,游標位置是可以直接讀寫的:讀出起點與終點,把新字串拼在中間,再把游標移到插入內容之後。舊版 IE 則要用 document.selection 建立 Range 物件,改它的 text 屬性。

完整程式碼

<!DOCTYPE html>
<html>
<head>
    <title>無標題頁</title>
    <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    (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);

    $(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").insertAtCaret($(this).val());
        });
    });
    </script>
</head>
<body>
    <span id="numd" style="border: 1px solid red; clear: both; display: inline-block;">
        <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" />
        </div>
    </span>
</body>
</html>

幾個實作細節

插入之後記得把 selectionStart 與 selectionEnd 一起往後移 myValue.length,否則游標會停在插入內容之前,使用者繼續打字就會顛倒順序。另外 scrollTop 要存回來,避免在長文字方塊中插入後視線跳掉。

最後那個 else 分支是保底方案:實在拿不到游標位置時,就附加到尾端,總比什麼都不做好。

希望這篇對大家的 jQuery 開發有幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved