使用 Javascript 向 textarea 添加标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9950894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:19:59  来源:igfitidea点击:

Adding tabs to textarea's using Javascript

javascripthtmltabstextarea

提问by Xeoncross

I would like to allow literal TAB(\t) characters in my textarea's. However, the TABkey cycles to the next form element. Since this is expected I don't want to break that standard. Likewise, CTRL + TABcycles browser tabs. Is there a recomended key-combo to allow people to enter an actual TABinside a textarea?

我想在我的 textarea 中允许文字TAB( \t) 字符。但是,TAB键会循环到下一个表单元素。由于这是预期的,我不想打破该标准。同样,CTRL + TAB循环浏览器选项卡。是否有推荐的组合键允许人们TAB在文本区域内输入实际值?

For example, CTRL + SPACEdoesn't seem to do anything, could that be used?

例如,CTRL + SPACE似乎没有做任何事情,可以使用吗?

Also, how would I listen for these combos correctly?

另外,我将如何正确收听这些组合?

回答by Mark Fraser

This is the code I use on textareas to keep the TAB from switching focus:

这是我在 textareas 上使用的代码,以防止 TAB 切换焦点:

$("textarea").keydown(function(e) {
  var $this, end, start;
  if (e.keyCode === 9) {
    start = this.selectionStart;
    end = this.selectionEnd;
    $this = $(this);
    $this.val($this.val().substring(0, start) + "\t" + $this.val().substring(end));
    this.selectionStart = this.selectionEnd = start + 1;
    return false;
  }
});

回答by Justin Copeland

You could

你可以

  1. Use say 4 spaces to emulate tabs.
  2. Have the user enter the "\t" character and treat it as a tab token to be parsed.
  1. 使用 4 个空格来模拟选项卡。
  2. 让用户输入“\t”字符并将其视为要解析的制表符。

回答by wjbryant

Tab Override(my own project) enables tab functionality in textareas and also allows custom key combinations to be used. This way, the Tab key can continue to be used as normal.

Tab Override(我自己的项目)在 textarea 中启用选项卡功能,还允许使用自定义组合键。这样,Tab 键可以继续正常使用。

回答by Dave Thomas

I've used Tabby jquery plugin to add this feature to a text area, works okay!

我已经使用 Tabby jquery 插件将此功能添加到文本区域,工作正常!

Tabby

虎斑

With it you can use tab key to insert tabs in a text area. Though you do lose tab changing elements functionality.

有了它,您可以使用制表键在文本区域中插入制表符。尽管您确实失去了选项卡更改元素功能。