欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

让文本框支持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