使用标签在textarea中缩进 [英] Use tab to indent in textarea
本文介绍了使用标签在textarea中缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的html textarea。现在,如果您单击其中的选项卡,它将转到下一个字段。我想让选项卡按钮缩进几个空格。我怎样才能做到这一点?感谢。
解决方案
$(document).delegate('#textbox','keydown',function(e){
var keyCode = e.keyCode || e .which;
if(keyCode == 9){
e.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd ;
// set textarea value to:text before caret + tab + text care after
$(this).val($(this).val()。substring(0,start )
+\ t
+ $(this).val()。substring(end));
//将插入符号再次放在正确的位置
this.selectionStart =
this.selectionEnd = start + 1;
}
});
http:// jsfiddle。 net / jz6J5 /
I have a simple html textarea on my side. Right now if you click tab in it, it goes to the next field. I would like to make the tab button indent a few spaces instead. How can I do this? Thanks.
解决方案
Borrowing heavily from other answers for similar questions (posted below)...
$(document).delegate('#textbox', 'keydown', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
e.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;
// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start)
+ "\t"
+ $(this).val().substring(end));
// put caret at right position again
this.selectionStart =
this.selectionEnd = start + 1;
}
});
jQuery: How to capture the TAB keypress within a Textbox
How to handle <tab> in textarea?
这篇关于使用标签在textarea中缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文