使用JS在textarea在光標處插入內容 – Javascript教程_JS教程_技術文章 – 程式設計聯盟

01
// 在光標處插入字符串
02
    // myField    文本框對象
03
    // myValue 要插入的值
04
    function insertAtCursor(myField, myValue)
05
    {
06
        //IE support
07
        if (document.selection)
08
        {
09
            myField.focus();
10
            sel            = document.selection.createRange();
11
            sel.text    = myValue;
12
            sel.select();
13
        }
14
        //MOZILLA/NETSCAPE support
15
        else if (myField.selectionStart || myField.selectionStart == '0')
16
        {
17
            var startPos    = myField.selectionStart;
18
            var endPos        = myField.selectionEnd;
19
            // save scrollTop before insert
20
            var restoreTop    = myField.scrollTop;
21
            myField.value    = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
22
            if (restoreTop > 0)
23
            {
24
                // restore previous scrollTop
25
                myField.scrollTop = restoreTop;
26
            }
27
            myField.focus();
28
            myField.selectionStart    = startPos + myValue.length;
29
            myField.selectionEnd    = startPos + myValue.length;
30
        } else {
31
            myField.value += myValue;
32
            myField.focus();
33
        }
34
    }
35
 
36
 
37
 
38
       function insertText() {
39
            var obj = document.getElementById("文本框");
40
            var str = "[#$%$#]插入的內容";
41
               if (document.selection) {
42
                   obj.focus();
43
                   var sel = document.selection.createRange();
44
 
45
                   sel.text = str;
46
 
47
               } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
48
                   var startPos = obj.selectionStart;
49
                   var endPos = obj.selectionEnd;
50
                    var tmpStr = obj.value;
51
                    obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
52
                } else {
53
                    obj.value += str;
54
            }
55
        }
56
 
57
 
58
 
59
//Jquery光標處插入文本
60
 
61
        $(document).ready(function () {
62
            $("#btnInsert").click(function () {
63
                var obj = $("#txtquestion").get(0);
64
                var str = "[#$%$#]";
65
                if (document.selection) {
66
                    obj.focus();
67
                    var sel = document.selection.createRange();
68
                    sel.text = str;
69
                } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
70
                    var startPos = obj.selectionStart;
71
                    var endPos = obj.selectionEnd;
72
                    var tmpStr = obj.value;
73
                    obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
74
                } else {
75
                    obj.value += str;
76
                
77
            });
78
        });

摘自 JSON

發佈留言