如何让标签按钮在HTML格式的textareas中工作?

时间:2017-02-05 16:52:47

标签: javascript html

我有一个带有textarea的HTML页面。我希望能够使用里面的标签按钮而不是切换焦点。我将this的代码包含在另一个问题的答案中,但该标签仍会切换焦点。但是,如果我在JavaScript控制台中运行相同的代码,它会更改选项卡按钮的行为以创建制表符。

我尝试的相关代码是:

function longOperation(error) {
  return new Promise < any > ((resolve, reject) => {
    if (error) {
      reject("Error")
    } else {
      setTimeout(function() {
        resolve(true)
      }, 1000)
    }

  })
}

let op = longOperation(true);

op
  .then(result => {
    console.log("Resolved #1")
  }, undefined)
  .then(result => {
    console.log("Resolved #2")
  }, undefined)
  .then(undefined, error => {
    console.log("Error1")
  })
  .then(undefined, error => {
    console.log("Error2")
  })
$("textarea").keydown(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();
  }
});

0 个答案:

没有答案