为什么这个文本区域似乎只添加一个空格而不是四个空格?

时间:2018-07-09 00:06:43

标签: javascript jquery textarea

这是我的代码:

doc.on("keydown", ".textarea_code_snippet", function(e) {
    if(e.keyCode === 9) { // tab was pressed

        // get caret position/selection
        var start = this.selectionStart;
        var end = this.selectionEnd;

        var $this = $(this);
        var value = $this.val();

        // set textarea value to: text before caret + tab + text
        // after caret 
        $this.val(value.substring(0, start)
                    + "\t"
                    + value.substring(end));

        // put caret at right position again (add one for the tab)
        this.selectionStart = this.selectionEnd = start + 1;

        // prevent the focus lose
        e.preventDefault();
    }
});

它处理<textarea>中的 Tab 键。当您按下 Tab 键时,它将\t附加到文本区域。现在,我想添加4个空格。这是我的新版本:

.
.
 $this.val(value.substring(0, start)
             + "    "
             + value.substring(end));
.
.

但是当我按 Tab 时,它仅将一个空间附加到文本区域。我该如何解决?

1 个答案:

答案 0 :(得分:1)

它确实添加了四个空格。您只是忘了调整一件事:

img[indices] = 1
indices_x = indices[:, 0]
indices_y = indices[:, 1]
img[np.ix_(indices_x,indices_y)] = 1

您可能会看到插入符号仅移动了一个空格。您需要将以上行更改为:

// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + 1;