让文本框支持Tab键缩进
程序员文章站
2022-06-17 18:25:31
...
var onTextareaKeydown = function(e){
if(e.keyCode == 9){
e.preventDefault();
var start = this.selectionStart, end = this.selectionEnd;
var text = this.value;
var tab = ' ';
text = text.substr(0, start) + tab + text.substr(start);
this.value = text;
this.selectionStart = start + tab.length;
this.selectionEnd = end + tab.length;
}
}
document.getElementById('textarea').onkeydown = onTextareaKeydown;
转载于:https://my.oschina.net/yecao/blog/174835