How to handle in textarea?

前端 未结 6 1407
青春惊慌失措
青春惊慌失措 2020-11-27 11:51

I would like a textarea that handles a situation of pressing tab key.

In default case if you press a tab key then focus leaves the textarea. But wh

6条回答
  •  误落风尘
    2020-11-27 12:19

    Found this while searching google. I made a really short one that can also indent and reverse indent selections of text:

        jQ(document).on('keydown', 'textarea', function(e) {
            if (e.keyCode !== 9) return;
            var Z;
            var S = this.selectionStart;
            var E = Z = this.selectionEnd;
            var A = this.value.slice(S, E);
            A = A.split('\n');
            if (!e.shiftKey)
                for (var x in A) {
                    A[x] = '\t' + A[x];
                    Z++;
                }
            else
                for (var x in A) {
                    if (A[x][0] == '\t')
                        A[x] = A[x].substr(1);
                    Z--;
                }
            A = A.join('\n');
            this.value = this.value.slice(0, S) + A + this.value.slice(E);
            this.selectionStart = S != E ? S : Z;;
            this.selectionEnd = Z;
            e.preventDefault();
        });
    

提交回复
热议问题