Keypress in jQuery: Press TAB inside TEXTAREA (when editing an existing text)

前端 未结 4 412
一个人的身影
一个人的身影 2020-12-30 12:47

I want to insert TAB characters inside a TEXTAREA, like this:


I can

相关标签:
4条回答
  • 2020-12-30 13:05

    Unfortunately, manipulating the text inside textarea elements is not as simple as one might hope. The reason that Tabby is larger than those simple snippets is that it works better. It has better cross-browser compatibility and handles things like tabbing selections.

    When minified, it's only about 5k. I'd suggest using it. You'll either have to discover and troubleshoot those same edge cases yourself anyway, or might not even know about them if users don't report them.

    0 讨论(0)
  • 2020-12-30 13:18

    I was creating a AJAX powered simple IDE for myself so I can rapidly test out PHP snippets.

    I remember stumbling upon the same problem, here's how I solved it:

    $('#input').keypress(function (e) {
        if (e.keyCode == 9) {
            var myValue = "\t";
            var startPos = this.selectionStart;
            var endPos = this.selectionEnd;
            var scrollTop = this.scrollTop;
            this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos,this.value.length);
            this.focus();
            this.selectionStart = startPos + myValue.length;
            this.selectionEnd = startPos + myValue.length;
            this.scrollTop = scrollTop;
    
            e.preventDefault();
        }
    });
    

    #input is the ID of the textarea.

    The code is not completely mine, I found it on Google somewhere.

    I've only tested it on FF 3.5 and IE7. It does not work on IE7 sadly.

    0 讨论(0)
  • 2020-12-30 13:24

    Yeah, dealing with input field selections across the different browsers is an annoyance, especially as in IE there are a few methods that look like they should work but actually don't. (Notably, combining using setEndPoint then measuring length, which looks OK until the selection starts or ends in newlines.)

    Here's a couple of utility functions I use to deal with input selections. It returns the value of the input split into bits that are before, inside and after the selection (with the selection counting as an empty string at the input focus position if it's not a selection). This makes it fairly simply to replace and insert content at the point you want, whilst taking care of the IE CRLF problem.

    (There may be a jQuery that does something like this, but I have yet to meet one.)

    // getPartitionedValue: for an input/textarea, return the value text, split into
    // an array of [before-selection, selection, after-selection] strings.
    //
    function getPartitionedValue(input) {
        var value= input.value;
        var start= input.value.length;
        var end= start;
        if (input.selectionStart!==undefined) {
            start= input.selectionStart;
            end= input.selectionEnd;
        } else if (document.selection!==undefined) {
            value= value.split('\r').join('');
            start=end= value.length;
            var range= document.selection.createRange();
            if (range.parentElement()===input) {
                var start= -range.moveStart('character', -10000000);
                var end= -range.moveEnd('character', -10000000);
                range.moveToElementText(input);
                var error= -range.moveStart('character', -10000000);
                start-= error;
                end-= error;
            }
        }
        return [
            value.substring(0, start),
            value.substring(start, end),
            value.substring(end)
        ];
    }
    
    // setPartitionedValue: set the value text and selected region in an input/
    // textarea.
    //
    function setPartitionedValue(input, value) {
        var oldtop= input.scrollTop!==undefined? input.scrollTop : null;
        input.value= value.join('');
        input.focus();
        var start= value[0].length;
        var end= value[0].length+value[1].length;
        if (input.selectionStart!==undefined) {
            input.selectionStart= start;
            input.selectionEnd= end;
            if (oldtop!==null)
                input.scrollTop= oldtop;
        }
        else if (document.selection!==undefined) {
            var range= input.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    }
    
    0 讨论(0)
  • 2020-12-30 13:24

    btw, see also:
    http://aspalliance.com/346_Tabbing_in_the_TextArea

    0 讨论(0)
提交回复
热议问题