使用 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
Adding tabs to textarea's using Javascript
提问by Xeoncross
I would like to allow literal TAB
(\t
) characters in my textarea's. However, the TAB
key cycles to the next form element. Since this is expected I don't want to break that standard. Likewise, CTRL + TAB
cycles browser tabs. Is there a recomended key-combo to allow people to enter an actual TAB
inside a textarea?
我想在我的 textarea 中允许文字TAB
( \t
) 字符。但是,TAB
键会循环到下一个表单元素。由于这是预期的,我不想打破该标准。同样,CTRL + TAB
循环浏览器选项卡。是否有推荐的组合键允许人们TAB
在文本区域内输入实际值?
For example, CTRL + SPACE
doesn'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
你可以
- Use say 4 spaces to emulate tabs.
- Have the user enter the "\t" character and treat it as a tab token to be parsed.
- 使用 4 个空格来模拟选项卡。
- 让用户输入“\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 键可以继续正常使用。