让文本框支持Tab键缩进

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;

你可能感兴趣的:(让文本框支持Tab键缩进)