如何按Enter键将选项卡级别保持在文本区域?

问题描述 投票:5回答:3

[当用户按下Enter时,我希望光标移动到新行,但是如果当前它们由两个选项卡缩进,则光标应保持缩进两个选项卡。

我已经实现了ignore tab事件来停止焦点在页面内移动,所以我现在只是在寻找使标签级别保持在新行上的逻辑。

if(e.keyCode === 13){

    //Logic here
}
javascript html
3个回答
5
投票

http://jsfiddle.net/DVKbn/

$("textarea").keydown(function(e){
    if(e.keyCode == 13){

        // assuming 'this' is textarea

        var cursorPos = this.selectionStart;
        var curentLine = this.value.substr(0, this.selectionStart).split("\n").pop();
        var indent = curentLine.match(/^\s*/)[0];
        var value = this.value;
        var textBefore = value.substring(0,  cursorPos );
        var textAfter  = value.substring( cursorPos, value.length );

        e.preventDefault(); // avoid creating a new line since we do it ourself
        this.value = textBefore + "\n" + indent + textAfter;
        setCaretPosition(this, cursorPos + indent.length + 1); // +1 is for the \n
    }
});

function setCaretPosition(ctrl, pos)
{

    if(ctrl.setSelectionRange)
    {
        ctrl.focus();
        ctrl.setSelectionRange(pos,pos);
    }
    else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}

0
投票

必须说基于一键的解决方案是晦涩的,因为人们也喜欢粘贴文本。请改用input事件。您可以像这样在jQuery中创建它:

$('textarea').on('input', function(e) {
var el = $(this);
var cur = $(this).prop('selectionStart'); // retrieve current caret position before setting value
var text = $(this).val();
var newText = text.replace(/^(.+)\t+/mg, '$1'); // remove intermediate tabs
newText = newText.replace(/^([^\t]*)$/mg, '\t\t$1'); // add two tabs in the beginning of each line

if (newText != text) { // If text changed...
	$(this).val(newText); // finally set value
	// and reset caret position shifted right by one symbol
	$(this).prop('selectionStart', cur + 1);
	$(this).prop('selectionEnd', cur + 1);
}
});
<textarea></textarea>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

顺便说一句,我太懒惰了,无法解释如何查看用户所需的制表符数量,这个仅在每一行插入两个制表符。


0
投票

我使用answer by Endless而不是修改execCommand 'insertText'改进了textarea.value

© www.soinside.com 2019 - 2024. All rights reserved.